日期:2014-05-20 浏览次数:20733 次
var result = table.GroupBy(g => g.id).Select(s => new { id = s.Key, id1 = s.Max(m => m.id1) }).ToList();
------解决方案--------------------
哦,刚才的LINQ查询是凭感觉写的,没有把握。现在我用Northwind来写了个简单的测试,查找某个用户的订单中最大的Freight值,发现我之前写的LINQ查询是正确的。代码和结果如下:
using (NorthwindEntities db = new NorthwindEntities()) { var result = db.Orders.GroupBy(g => g.CustomerID).Select(s => new { cid = s.Key, max = s.Max(m => m.Freight) }).ToList(); result.ForEach(e => Console.WriteLine("ID:{0}, Freight:{1}", e.cid, e.max)); }
------解决方案--------------------
var query = from x in db.table1 group x by x.id into g select new { id = g.id, id1 = g.Max(y => y.id1) };
------解决方案--------------------
var result=db.Table1.GroupBy(t=>t.id).Select(t=>new{id=t.Key,id1=t.Max(s=>s.id1)});
------解决方案--------------------
var s = entities.table1.GroupBy(c => c.id).OrderBy(c=>c.Key).Select(m => new { m.Key, Max= m.Max(g => g.id1) });
------解决方案--------------------
唉,怎么像看天书一样。。。。