开发者

Aggregation Query w/ Linq to Entities

I'm trying to query the Northwind database relationship开发者_如何学Go between Categories and Products, where each category has multiple products...

Im looking for a query that will return the 1 category with the highest number of products in it.

This is as far as I've gotten

       var results = from c in entities.CategorySet
                      orderby c.Products.Count descending                         
                      select new { 
                          CategoryName = c.CategoryName, 
                          ProductCount = c.Products.Count 
                      };

        var result = results.Take(1).First();

is there a more effective way?


What about this:

var result = (from c in entities.CategorySet
                      orderby c.Products.Count descending                         
                      select new { 
                          CategoryName = c.CategoryName, 
                          ProductCount = c.Products.Count 
                      }).First();


Using this

var results = (from c in entities.CategorySet
               orderby c.Products.Count descending
               select new {c.CategoryName, ProductCount = c.Products.Count }).Take(1);

Is about the same as you would have done it using a Sql Statement. So I dont see a more effective way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜