简体   繁体   English

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

[英]How to use having ,group by and distinct in linq or lambda expression

I need to convert the follwing query into linq or lambda expression 我需要将以下查询转换为linqlambda表达式

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

So Far I have tried 我到目前为止已经尝试过

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();

I think this code works. 我认为这段代码有效。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM