开发者

Aggregation Query w/ Linq to Entities

开发者 https://www.devze.com 2022-12-14 02:57 出处:网络
I\'m trying to query the Northwind database relationship开发者_如何学Go between Categories and Products, where each category has multiple products...

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

精彩评论

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