having and conditional count() in linq query

后端 未结 1 898
醉酒成梦
醉酒成梦 2021-01-12 17:02

I want to create this query:

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


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 17:39

    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 : This would give you the result as an 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();
    

    (note that it also fills the dt table)

    0 讨论(0)
提交回复
热议问题