带有in子句和max的Linq查询

时间:2017-06-05 20:53:06

标签: linq max

我有一个简单的SQL查询,但我无法将其转换为LINQ:

select * from client_software where update_date in 
    (select max(update_date) AS UPDATE_DATE from client_software a
        where a.client_code = client_software.client_code and
            a.software_code = client_software.software_code) 

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

请你试一试:

var q = entity.client_software.Where(r => r.update_date.Value
== entity.client_software.Where(u => u.client_code == r.client_code &&
u.software_code == r.software_code).Max(s => s.update_date)).ToList(); 

答案 1 :(得分:0)

var ans = from cs in client_software
          where cs.update_date == (from a in client_software where a.client_code == cs.client_code && a.software_code == cs.software_code select a).Max(cs2 => cs2.update_date)
          select cs;

在这种情况下,使用group by可能更清楚:

var ans2 = from cs in client_software
          group cs by new { cs.client_code, cs.software_code } into csg
          from cs in csg
          where cs.update_date == csg.Max(cs => cs.update_date)
          select cs;