LINQ笔记


1、获取日期最大的订单列表

var backs = _installRepository.GetList();
var grouplist = backs.GroupBy(f => f.OrderCode).Select(g => new { OrderCode = g.Key, CreateTime = g.Max(f => f.CreateTime) });

//自关联方法1
var ret = backs.Join(grouplist,
o => new { f1 = o.OrderCode, f2 = o.CreateTime },
i => new { f1 = i.OrderCode, f2 = i.CreateTime },
(o, i) => new { o, i })
.Select(g => g.o);

var list = ret.ToList();

//自关联方法2
var ret2 = from t in backs
join t2 in grouplist
on new { t.OrderCode, t.CreateTime } equals new { t2.OrderCode, t2.CreateTime }
select t;