How to distinguish Linq Dynamic Query result as a custom class?

I usually do this:

var a = from   p in db.Products
        where  p.ProductType == "Tee Shirt"
        group  p by p.ProductColor into g
        select new Category { 
               PropertyType = g.Key,
               Count = g.Count() }

But I have a code like this:

var a = Products
        .Where("ProductType == @0", "Tee Shirt")
        .GroupBy("ProductColor", "it")
        .Select("new ( Key, it.Count() as int )");

What syntax can be changed to get the same results, i.e. How to make a projection of a category from the second Linq operator?

I know both in g and in the same and represents the whole record of the table, and that I pull the whole record just to make an account. I also need to fix this. Edit: Marcelo Cantos noted that Linq is smart enough not to pull out unnecessary data. Thank!

+5
source share
1 answer

Why would you do this? Since you still have information after calling GroupBy, you can easily do this:

var a = Products
        .Where("ProductType == @0", "Tee Shirt")
        .GroupBy("ProductColor", "it")
        .Select(c => new Category { 
            PropertyType = g.Key, Count = g.Count() 
        });

, / , .

+1

All Articles