如何在linq或lambda表达式中使用having,group by和distinct

时间:2016-03-03 05:47:07

标签: c# sql entity-framework linq

我需要将以下查询转换为 linq lambda 表达式

 SELECT count(Hid.HouseholdID) FROM
 (SELECT HouseholdID FROM tblTmpJoinAll 
 WHERE loyal = 1 Group By HouseholdId 
 HAVING Count(distinct BasketID)>1) as Hid;

到目前为止,我已经尝试了

lst.Where(f => f.Flybuys == true)
.GroupBy(h=>h.HouseholdID)
//.Where(x=>x.)// don't know what to right here
.Select(h => h.Key).Count();

1 个答案:

答案 0 :(得分:3)

我认为此代码有效。

lst.Where(f => f.Flybuys) // you don't need == true here
   .GroupBy(f => f.HouseholdID)
   .Where(f => f.GroupBy(i => i.BasketID).Count() > 1)
   .Count();