2013-05-15 74 views
0

我想優先選擇獨特的情侶(發情時間,到達時間,旅遊類別),在表格中以較低的價格粗體顯示最佳記錄。在c#列表中選擇具有最大值的特定記錄

這是低於代碼的結果。

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** 

非常感謝你

+0

那你試試? – nvoigt

回答

0

你應該能夠做到像下面這樣:

var result = yourRepo.OrderBy(p => p.Price) 
        .ThenBy(n => n.Name) 
        .Take(stuff.Select(q => q.Name) 
           .Distinct() 
           .Count()); 
2
.GroupBy(m => new { 
    m.DepartureTime, 
    m.ArrivalTime, 
    m.TravelClass 
}) 
.Select(g => g.OrderBy(x => x.Price).First()); 
+0

+1好的解決方案。 –

0

以同樣的方式,我張貼在這裏:https://stackoverflow.com/a/16567385/2385557

試試這個:

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年1月8日9時13分○○秒,抵達2013年1月8日10:14:00,並在旅行類經濟成本2000

與出發最便宜的航班在2013年1月8日9時13分00秒,抵達2013年1月8日10:14:00,並在旅遊類第一成本3600 01/08

最便宜的航班起飛與/ 2013 10:11:00,到達01/08/2013 11:14:00並在旅行類經濟成本2000

與出發最便宜的航班在2013年1月8日10:11:00,抵達2013年1月8日11:14:00,並在旅遊類第一成本2800

相關問題