2009-08-26 58 views
6

我想创建此查询:有和条件计数()在LINQ查询

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

我开始用这样的:

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

回答

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 }; 

UPDATE:这会给你结果为IQueryable<DataRow>

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表)

+0

非常感谢你,我 需要的结果作为IQueryable的, 有没有一种方法来创建“选择”的结果作为一个IQueryable的?或者我需要手动创建行? – Rodniko 2009-08-26 10:42:10

+0

查看更新的答案 – 2009-08-26 12:19:26

+0

非常感谢你:) – Rodniko 2009-08-26 13:15:19