简体   繁体   English

Linq Join-分组依据-OrderByDescending查询

[英]Linq join - group by - OrderByDescending query

I have this query: 我有这个查询:

from i in dc.TFFiles                    
where i.Tid == tid
group i by new { i.Title, i.Type } into gr
select gr.OrderByDescending(g => g.Version).First();

I want to add a join with another table so I will have access to a field of the second table 我想添加与另一个表的联接,这样我就可以访问第二个表的字段

Here is the join I need without the group by : 这是我需要的加入,没有group by

from i in dc.TFFiles
join c in dc.FCategory on i.CatId equals c.id 
....

How I can combine the group by query with the join 我怎么可以结合group by查询与join

Tried like this but it doesn't order the results by Version descending. 尝试过这种方法,但是它不会按版本降序对结果进行排序。 I always get Version 1 even though there are files with higher versions. 即使有更高版本的文件,我也始终会获得版本1。

var result =
                    from i in dc.TFFiles                    
                    where i.Tid == tid                    
                    group i by new { i.Title, i.Type } into gr
                    orderby gr.FirstOrDefault().Version descending
                    join c in dc.FCategories on gr.FirstOrDefault().CatId equals c.Id
                    select new { id = gr.FirstOrDefault().id, Title = gr.FirstOrDefault().Title, Version = gr.FirstOrDefault().Version };

Maybe you can try that : 也许您可以尝试:

var result =
                    from i in dc.TFFiles                    
                    where i.Tid == tid                    
                    group i by new { i.Title, i.Type } into gr
                    join c in dc.FCategories on gr.FirstOrDefault().CatId equals c.Id
                    select new { id = gr.FirstOrDefault().id, Title = gr.FirstOrDefault().Title, Version = (from cv in gr select cv.Version).Max() };

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

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