开发者

LINQ ordering by highest value per unique value in a certain column

I'm trying to figure out how to select the leading bids in a table called bids using linq.

The results needs to take the highest bid for each unique userId and display that ordered by bidValue

bids table:

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

outcome:

bidId 6, userid 1开发者_如何学Go, bidValue 50
bidId 5, userid 2, bidValue 40
bidId 3, userid 3, bidValue 20


Something like this should work

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜