2009-08-26 100 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