简体   繁体   English

Linq查询具有分组且不同的嵌套选择语句

[英]Linq query for a nested select statement with grouping and distinct

I'd like to translate the following SQL statement into a linq query: 我想将以下SQL语句转换为linq查询:

select COUNT(*), itemid,  globalid, title, preview, previewimage, previewimage_alt, link  
from (
        select distinct Id, itemid,  globalid, title, preview, previewimage, previewimage_alt,
               (select top 1 link from LikeCounter where GlobalId=x.GlobalId) as link
        from [LikeCounter] x
        where PortalId=1 and LanguageId=1
    ) as t
GROUP BY itemid, globalid, title, preview, previewimage, previewimage_alt, link
ORDER BY COUNT(*) desc

The query is over a view that holds records of objects being "liked". 查询是在一个视图上进行的,该视图保存了“喜欢”的对象的记录。 Since the objects can be published in multiple places, and the view was setup to allow for filtering for a certain place, it requires a distinct before grouping the records to find out the view count (that's the reason for the additional query for the "link" column). 由于可以在多个位置发布对象,并且已将视图设置为允许对某个位置进行过滤,因此在对记录进行分组以找出视图计数之前,需要一个不同的视图(这就是对“链接”进行附加查询的原因“列)。

Is a nested SELECT statement possible in one linq statement? 在一个linq语句中是否可以嵌套SELECT语句?

The inner query is no problem: 内部查询没问题:

(from x in LikeCounter
where x.PortalId==1 && x.LanguageId==1  
select new {x.Id, x.ItemId, x.GlobalId, x.LanguageId, x.Title, x.Preview, x.PreviewImage_alt, 
              Morelink=(from y in LikeCounter
              where y.GlobalId==x.GlobalId
              select y.Morelink).FirstOrDefault()
           }).Distinct()

But is there a way to extend this with the grouping of the distinct records, that results in just one query to the database ? 但是,是否有一种方法可以通过对不同的记录进行分组来扩展它,从而导致对数据库的一个查询?

Thanks in advance for any input... 预先感谢您的任何投入...

Nina 妮娜

Edit: 编辑:

the following query almost returns what I want -- but produces multiple queries to the SQL server: 以下查询几乎返回了我想要的内容-但对SQL Server产生了多个查询:

(from y in 
((from x in LikeCounter
where x.PortalId==1 && x.LanguageId==1 
select new {x.Id, x.ItemId, x.GlobalId, x.LanguageId, x.Title, x.Preview, x.PreviewImage_alt, 
              Link=(from y in Xparo_LikeCounter
              where y.GlobalId==x.GlobalId
              select y.Link).FirstOrDefault()
           }).Distinct())
group y by y.GlobalId into grp
select new {Data=grp, Count= grp.Count()}).OrderByDescending (x => x.Count)

I Think the below should work but i can't really test it. 我认为以下内容应该可以工作,但我无法真正对其进行测试。 No idea how many queries it would take either 不知道要花多少个查询

from subq in (from x in LikeCounter
              where x.PortalId==1 && x.LanguageId==1  
              select new {x.Id, x.ItemId, x.GlobalId, x.LanguageId, x.Title, x.Preview, x.PreviewImage_alt, 
              Morelink=(from y in LikeCounter
              where y.GlobalId==x.GlobalId
              select y.Morelink).FirstOrDefault()
              }).Distinct()
group subq by new {TheCount = subq.Id.Count(), subq.Id, subq.ItemId, subq.GlobalId, subq.LanguageId, subq.Title, subq.Preview, subq.PreviewImage_alt, subq.Morelink } into grouped
order by grouped.TheCount descending;

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

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