我将如何将以下T-SQL语句翻译为Linq

时间:2014-02-07 16:17:39

标签: c# sql linq entity-framework

这是我的sql语法

select DepartmentId, max(fromdate) from iwof.DateSnippet group by DepartmentId 

这是我得到的结果

1   2013-09-09 14:53:52.813 
2   2014-02-07 14:37:21.740
3   2014-02-07 14:37:21.737
4   2014-02-07 14:37:21.740
5   2014-02-07 14:37:21.743
6   2014-02-07 14:37:21.743

1 个答案:

答案 0 :(得分:1)

您使用的语法不正确。格式更接近这个,它应该做的伎俩

var results = from p in iwof.DateSnippet
          group p by p.DepartmentId into g
          select new { DepartmentId = g.Key, MaxFromDate = g.Max(fromdate) };