在c#列表中选择具有最大值的特定记录

本文关键字:最大值 记录 列表 选择 | 更新日期: 2023-09-27 18:13:49

我想为每一对独特的组合(出发时间,到达时间,旅行舱位)优雅地选择,表中粗体显示的价格较低的最佳记录。

这是在给我带来结果行的代码下面。

tripRepository.FindBy(x => x.SearchID == id).Include(b => b.Results)

- DepartureTime          ArrivalTime            TravelClass  Price 
====================================================================================
- 01/08/2013 09:13:00    01/08/2013 10:14:00     economy    4700
- 01/08/2013 09:13:00    01/08/2013 10:14:00     first      8300
- 01/08/2013 09:13:00    01/08/2013 10:14:00     economy    2750
- **01/08/2013 09:13:00  01/08/2013 10:14:00     first      3600**
- **01/08/2013 09:13:00  01/08/2013 10:14:00     economy    2000**
- 01/08/2013 10:11:00    01/08/2013 11:14:00     economy    4700
- 01/08/2013 10:11:00    01/08/2013 11:14:00     first      8300
- 01/08/2013 10:11:00    01/08/2013 11:14:00     economy    2750
- 01/08/2013 10:11:00    01/08/2013 11:14:00     first      2950
- **01/08/2013 10:11:00  01/08/2013 11:14:00     economy    2000**
- **01/08/2013 10:11:00  01/08/2013 11:14:00     first      2800**

非常感谢

在c#列表中选择具有最大值的特定记录

.GroupBy(m => new {
   m.DepartureTime,
   m.ArrivalTime,
   m.TravelClass
})
.Select(g  => g.OrderBy(x => x.Price).First());

您应该能够执行如下操作:

var result = yourRepo.OrderBy(p => p.Price)
                     .ThenBy(n => n.Name)
                     .Take(stuff.Select(q => q.Name)
                                .Distinct()
                                .Count());

与我在这里发布的方式相同:https://stackoverflow.com/a/16567385/2385557

try this:

public class Test
{
    public void Exec()
    {
        var items = new List<Item>{ 
        new Item { DepartureTime = "01/08/2013 09:13:00", ArrivalTime = "01/08/2013 10:14:00", TravelClass="economy" , Price =        4700 },
        new Item { DepartureTime = "01/08/2013 09:13:00", ArrivalTime = "01/08/2013 10:14:00", TravelClass="first"   , Price =        8300 },
        new Item { DepartureTime = "01/08/2013 09:13:00", ArrivalTime = "01/08/2013 10:14:00", TravelClass="economy" , Price =        2750 },
        new Item { DepartureTime = "01/08/2013 09:13:00", ArrivalTime = "01/08/2013 10:14:00", TravelClass="first"   , Price =        3600 },
        new Item { DepartureTime = "01/08/2013 09:13:00", ArrivalTime = "01/08/2013 10:14:00", TravelClass="economy" , Price =        2000 },
        new Item { DepartureTime = "01/08/2013 10:11:00", ArrivalTime = "01/08/2013 11:14:00", TravelClass="economy" , Price =        4700 },
        new Item { DepartureTime = "01/08/2013 10:11:00", ArrivalTime = "01/08/2013 11:14:00", TravelClass="first"   , Price =        8300 },
        new Item { DepartureTime = "01/08/2013 10:11:00", ArrivalTime = "01/08/2013 11:14:00", TravelClass="economy" , Price =        2750 },
        new Item { DepartureTime = "01/08/2013 10:11:00", ArrivalTime = "01/08/2013 11:14:00", TravelClass="first"   , Price =        2950 },
        new Item { DepartureTime = "01/08/2013 10:11:00", ArrivalTime = "01/08/2013 11:14:00", TravelClass="economy" , Price =        2000 },
        new Item { DepartureTime = "01/08/2013 10:11:00", ArrivalTime = "01/08/2013 11:14:00", TravelClass="first"   , Price =        2800 },
    };
        var result = items
                        .GroupBy(groupedItems => new { groupedItems.DepartureTime, groupedItems.ArrivalTime, groupedItems.TravelClass })
                        .SelectMany(i => items
                                             .Where(innerItem =>
                                                                    (innerItem.DepartureTime == i.Key.DepartureTime
                                                                    && innerItem.ArrivalTime == i.Key.ArrivalTime
                                                                    && innerItem.TravelClass == i.Key.TravelClass)
                                                                    && innerItem.Price == i.Min(ii => ii.Price))
                                             .Select(innerItem => innerItem)
                                             );
        foreach (var item in result)
            Console.WriteLine("Cheapest flight with departure at {0}, arrival at {1} and in travel class {2} costs {3}", item.DepartureTime, item.ArrivalTime, item.TravelClass, item.Price);
    }
    private class Item
    {
        public String DepartureTime { get; set; }
        public String ArrivalTime { get; set; }
        public String TravelClass { get; set; }
        public Int32 Price { get; set; }
    }
}

输出:

出发时间为2013年8月1日09:13:00,到达时间为2013年8月1日10:14:00,经济舱最便宜机票价格为2000

出发时间为2013年8月1日09:13:00,到达时间为2013年8月1日10:14:00,头等舱最便宜机票价格为3600

出发时间为2013年8月1日10:11:00,到达时间为2013年8月1日11:14:00,经济舱最便宜机票价格为2000

出发时间为2013年8月1日10:11:00,到达时间为2013年8月1日11:14:00,头等舱最便宜机票2800