将SQL转换为LINQ,按内部表分组

时间:2011-01-27 13:35:01

标签: sql linq-to-sql

有人可以帮助将其转换为linq吗?

SELECT x.contentobjectid, Min(x.city) as city, temp.startdate
from calendarentry as x
    inner join (select contentobjectid, startdate from calendarentry where startdate > getdate() group by contentobjectid, startdate) as temp
    on temp.contentobjectid = x.contentobjectid
group by x.contentobjectid, temp.startdate

即时使用实体框架

感谢

1 个答案:

答案 0 :(得分:1)

如果没有你的数据库模式,很难真正做到这一点,但尝试我所写的作为起点。另外,我强烈建议下载LINQPad,支付30美元用于智能感知支持!

var result = (
from c1 in calendarentry
let grp = (from c2 in calendarentry
             where c2.contentobjectid = c1.contentopjectid &&
                   c2.startdate > DateTime.Now
             group c2 by new { contentobjectid = c2.contentobjectid, startdate = c2.startdate } into g
             select g)
select new {
         contentobjectid = c1.contentobjectid, 
         city = c1.Min(x => x.city),
         startdate = grp.startDate
       });