Monday, June 30, 2008

LINQ OrderBy

I'm extremely new to LINQ, okay?...

But today I discovered, this...

var currentValues = from c in metricsEntities.CurrentMetrics
                    join m in metricsEntities.MetricsSet
                        on c.MetricName equals m.MetricName
                    where m.HideFromView == false
                    orderby m.DisplayOrder ascending
                    select new { c, m }
                    ;

 

is very different to this...

var currentValues = from c in metricsEntities.CurrentMetrics
                    join m in metricsEntities.MetricsSet
                        on c.MetricName equals m.MetricName
                    orderby m.DisplayOrder ascending
                    where m.HideFromView == false
                    select new { c, m }
                    ;

The first query works like I expected, ordering my list properly but the second one (which is where I had started) simply seems to ignore the order.

It's all to do with the "order" of the "where" and "orderby" clauses. I suppose it makes sense really. The latter query orders first, the evaluates the where clause, but the orderby seems to get lost....

Ar well, you live and learn...