如何在LINQ中组合Where子句和group by [英] How to combine Where clause and group by in LINQ
本文介绍了如何在LINQ中组合Where子句和group by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以帮助我将下面的代码转换为LINQ吗?
Can any one help me convert the below code to LINQ?
Select Catg,Count(*) From Mycatg where IsPublic=1 or FirstName='XXX' Group By Catg .
推荐答案
在C#中,类似:
var query = from category in mycatg
where category.IsPublic == 1
|| category.FirstName == "XXX"
group 1 by category.Catg into grouped
select new { Catg = grouped.Key,
Count = grouped.Count() };
"1"的投影清楚地表明,我们需要的只是分组和计数的关键-每个分组中的各个条目都是不相关的.
The projection of "1" makes it clear that all we need is the key of the grouping and the count - the individual entries in each grouping are irrelevant.
使用lambda语法和点表示法:
Using lambda syntax and dot notation:
var query = mycatg.Where(category => category.IsPublic == 1
|| category.FirstName == "XXX")
.GroupBy(category => category.Catg,
category => 1)
.Select(grouped => new { Catg = grouped.Key,
Count = grouped.Count() });
这篇关于如何在LINQ中组合Where子句和group by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文