当前位置:  开发笔记 > 小程序 > 正文

在linq查询中有条件count()

如何解决《在linq查询中有条件count()》经验,为你挑选了1个好方法。

我想创建此查询:

select Something, count(Something) as "Num_Of_Times"
from tbl_results
group by Something
having count(Something)>5

我从这开始:

tempResults.GroupBy(dataRow => dataRow.Field("Something"))
   .Count() //(.......what comes here , to make Count()>5?)

Thomas Leves.. 8

from item in tbl_results
group item by item.Something into groupedItems
let count = groupedItems.Count()
where count > 5
select new { Something = groupedItems.Key, Num_Of_Times = count };

更新:这会给你一个结果IQueryable:

DataTable dt= new DataTable();
dt.Columns.Add("Something", typeof(int));
dt.Columns.Add("Num_Of_Times", typeof(int));

var results =   (from item in tbl_results
                 group item by item.Something into groupedItems
                 let count = groupedItems.Count()
                 where count > 2
                 select dt.Rows.Add(groupedItems.Key, count)).AsQueryable();

(注意它也填充了dt表)



1> Thomas Leves..:
from item in tbl_results
group item by item.Something into groupedItems
let count = groupedItems.Count()
where count > 5
select new { Something = groupedItems.Key, Num_Of_Times = count };

更新:这会给你一个结果IQueryable:

DataTable dt= new DataTable();
dt.Columns.Add("Something", typeof(int));
dt.Columns.Add("Num_Of_Times", typeof(int));

var results =   (from item in tbl_results
                 group item by item.Something into groupedItems
                 let count = groupedItems.Count()
                 where count > 2
                 select dt.Rows.Add(groupedItems.Key, count)).AsQueryable();

(注意它也填充了dt表)

推荐阅读
小白也坚强_177
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有