本文介绍了C#linq查询与groupby,有和哪里。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi,

How to write Linq Query for below SQL Query.

Select Count(ul.OriginId),ul.OriginId from UserLink ul
Where ul.OriginId is not null
group by ul.OriginId
having count(ul.OriginId)>1


Select ul.UserId from UserLink ul where ul.OriginId=123

Finally I want to return UserId group by OriginId but here I have written in two separate queries. 
Can we write above sql queries in single Linq query? please help me how to write  linq query in C#.

Thanks,
jayaram





我尝试了什么:



我无法在Linq中使用。



What I have tried:

I am not able to use having in Linq.

推荐答案

var data = context.UserLink
    .GroupBy(x=>x.OriginId)
    .Where(grp=>grp.Key==123 && grp.Count()>1)
    .SelectMany(grp=>new
        {
             OriginId = grp.Key,
             UserId = grp.Select(u=> u.UserId)
             CountOfOriginId = grp.Count(),
         })
    .ToList();





最后,我建议:

1)dowload []

2)下载 []


这篇关于C#linq查询与groupby,有和哪里。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 19:28