在查询语法中将sql转换为实体

时间:2013-05-17 07:42:23

标签: asp.net entity-framework asp.net-mvc-4 entity

如何在查询语法中将以下查询转换为实体框架。

  

SELECT MIN(Date)StartDate,MAX(Date)EndDate,Title,Flag FROM   假日GROUP BY标题,国旗

任何帮助?

2 个答案:

答案 0 :(得分:1)

经过一段艰难时期后我得到了解决方案

(from e in db.Holidays
 group e by new {e.Title, e.Flag}
 into g
 select new AllEventViewModel
 {
 start = g.Max(e=>e.Date),
 end = g.Min(e => e.Date),
 title = g.Key.Title,
 Flag = g.Key.Flag
 }).AsEnumerable();

答案 1 :(得分:0)

var results = db.holidays.GroupBy(g => new { g.Title, g.Flag }).Select(k => new
{
    MinDate = k.Min(a => a.Date),
    StartDate = k.FirstOrDefault().StartDate,
    MaxDate = k.Max(b => b.Date),
    EndDate = k.FirstOrDefault().EndDate,
    Title = k.Key.Title,
    Flag = k.Key.Flag
}).ToList();