0

我已经尝试了以下数据的各种分组选项,以便仅获得每辆车的最高出价记录,但没有成功。我想我忽略了显而易见的:-)

bidId, carID, bidamount, dateplaced, userID  
45 83 650 5/11/2012 4:15:07 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
43 83 625 5/11/2012 4:12:51 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
41 83 600 5/11/2012 3:36:31 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
39 83 575 2/11/2012 4:06:57 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
56 83 575 9/11/2012 10:35:25 a.m. 0b3c2ba5-1538-4557-a6c0-7de701fd83e7   
52 83 550 8/11/2012 1:40:37 p.m. 0b3c2ba5-1538-4557-a6c0-7de701fd83e7   
38 83 550 2/11/2012 3:58:53 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
37 83 525 2/11/2012 3:58:19 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
49 83 525 7/11/2012 1:40:37 p.m. 0b3c2ba5-1538-4557-a6c0-7de701fd83e7   
80 86 750 21/11/2012 3:17:53 p.m. 0b3c2ba5-1538-4557-a6c0-7de701fd83e7   
69 86 725 20/11/2012 2:09:52 p.m. 0b3c2ba5-1538-4557-a6c0-7de701fd83e7   
68 86 700 20/11/2012 2:09:33 p.m. 0b3c2ba5-1538-4557-a6c0-7de701fd83e7   
59 86 675 13/11/2012 2:38:12 p.m. 0b3c2ba5-1538-4557-a6c0-7de701fd83e7   
53 86 650 8/11/2012 10:35:25 a.m. 0b3c2ba5-1538-4557-a6c0-7de701fd83e7   
47 86 625 5/11/2012 5:45:08 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
46 86 600 5/11/2012 5:45:04 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
44 86 575 5/11/2012 4:15:02 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
42 86 550 5/11/2012 3:36:40 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
40 86 525 5/11/2012 3:33:36 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
66 89 675 15/11/2012 4:21:40 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
65 89 650 15/11/2012 4:21:36 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
64 89 625 15/11/2012 4:21:15 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
63 89 600 15/11/2012 4:18:10 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
62 89 575 15/11/2012 4:17:57 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
61 89 550 15/11/2012 4:17:54 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   
60 89 525 15/11/2012 4:17:51 p.m. a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc   

我不知道为什么这里的一些有经验的用户投票决定关闭它,因为@dbaseman 几乎回答了我的问题并理解了我在问什么。请重新打开,以便@dbaseman 可以完成他的答案。

4

2 回答 2

3

您可以通过以下方式获取具有汽车 ID 和最高出价的匿名对象:

  1. 分组依据carID
  2. 选择分组键+ max bybidamount

var maxBids = collection.GroupBy(item => item.carID)
    .Select(grp => new { 
        carId = grp.Key, 
        maxBid = grp.Max(grpitems => grpitems.bidamount) 
     });

如果您愿意,可以从那里轻松转换为字典:

Dictionary<string, double> maxBidsByCar = 
    maxBids.ToDictionary(item => item.carId, item => item.maxBid);

编辑

由于您需要用户,因此查询必须与上面有所不同——这里我按出价金额和放置日期排序,并取了第一个项目。

var maxBids = collection.GroupBy(item => item.carID)

     // select the max bids (here maxBid is the entire row)
    .Select(grp => new { 
        carId = grp.Key, 
        maxBid = grp.OrderByDescending(grpitems => grpitems.bidamount)
                      .ThenBy(grpitems => grpitems.dateplaced).First()
     })

     // flatten the query to just get the max bid amount and user ID
     .Select(item => new {
         carId = item.carId,
         maxBid = maxBid.bidamount,
         userId = maxBid.userID
     });
于 2012-11-27T19:36:53.063 回答
1

试试这个:

var q = from n in table
        group n by n.bidamount, n.carID into g
        select g.OrderByDescending(t=>t.bidamount).First();
于 2012-11-27T19:37:04.110 回答