通常,我会这样做:
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() }
但是我有这样的代码:
var a = Products
.Where("ProductType == @0", "Tee Shirt")
.GroupBy("ProductColor", "it")
.Select("new ( Key, it.Count() as int )");
我该如何更改语法以产生相同的结果,即如何从第二个Linq语句中投影Category?我知道在这两个语句中,g和it是相同的,并且代表整个表记录,而我只是为了计数而提取整个记录。我也需要解决这个问题。编辑:Marcelo Cantos指出,Linq足够聪明,不会提取不必要的数据。谢谢!