linq group by max 多表链接实例

        SELECT s.* FROM dbo.ERG_TipOffsInfo s,
        (SELECT Data,MAX(Createtime) max_Time FROM dbo.ERG_TipOffsInfo GROUP BY Data) t
        WHERE s.Data=t.Data AND s.CreateTime = t.max_Time AND s.TipOffsTypeId=2
        ORDER BY s.Createtime DESC

  linq 改写代码

       var list = from s in context.ERG_TipOffsInfo
                       join l in
                           (
                               from t in context.ERG_TipOffsInfo
                               group t by t.Data into g
                               select new
                                {
                                    Data = g.Key,
                                    CreateTime = g.Max(p => p.CreateTime)
                                }
                            )
                        on s.Data equals l.Data
                       where s.CreateTime.Equals(l.CreateTime)
                       orderby l.CreateTime descending
                       select new TopMoreTimeTipOffsInfo { Data = l.Data, Id = s.TipOffsInfoId, TipOffsTypeId = s.TipOffsTypeId };
            if (tipOffsTypeId > 0)
            {
                list = list.Where(p => p.TipOffsTypeId.Equals(tipOffsTypeId));
            }

  

原文地址:https://www.cnblogs.com/liuxiutianxia/p/4666886.html