我正在尝试弄清楚如何使用linq在名为出价的表格中选择主要出价。
结果需要对每个唯一的userId采用最高出价并显示按bidValue排序
出价表:
bidId 1, userid 1, bidValue = 10
bidId 2, userid 2, bidValue = 20
bidId 3, userid 3, bidValue = 20
bidId 4, userid 1, bidValue = 30
bidId 5, userid 2, bidValue = 40
bidId 6, userid 1, bidValue = 50
结果:
bidId 6, userid 1, bidValue 50
bidId 5, userid 2, bidValue 40
bidId 3, userid 3, bidValue 20
答案 0 :(得分:2)
这样的事情应该有效
var query = (from bid in context.Bids
group bid by bid.userid into bidg
select new
{
bidId = bidg.OrderByDescending(b => b.bidValue).First().bidid,
userid = bidg.Key,
bidValue = bidg.Select(b => b.bidValue).Max()
}).OrderByDescending(b => b.bidvalue);