views:

31

answers:

2

I'm trying to query the Northwind database relationship 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?

+1  A: 

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();
Ngu Soon Hui
A: 

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.

astander