返回每辆车的最高出价

时间:2012-11-27 19:33:35

标签: c# linq

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

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可以完成他的答案。

2 个答案:

答案 0 :(得分:3)

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

  1. carID分组
  2. bidamount
  3. 选择分组键+最大值
    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
         });
    

答案 1 :(得分:1)

试试这个:

var q = from n in table
        group n by n.bidamount, n.carID into g
        select g.OrderByDescending(t=>t.bidamount).First();