2017-12-13 28 views

답변

2
items = items.GroupBy(x => x.category) 
    .Select(x => new Item(x.Key, x.Select(y => y.price).Min())) 
    .ToList(); 
1

이 작업을 수행 할 수 있습니다.

foreach (var item in items.GroupBy(s => s.Category)) 
{ 
    var cheaper = item.Min(x => x.Price); 
    //you can add it to a list of cheaper items 
} 

죄송합니다. 제거 물건을 건너 뛰었습니다. max를 사용하여 동일한 로직을 수행 한 후 전체 가격을 비교하여 해당 가격과 일치하지 않는 로직 만 가져올 수 있습니다. 같은 foreach 문 안에 모든 것을 넣을 수 있습니다. 가르치는 목적으로 만 다른 foreach에 넣는 이유입니다.

foreach (var item in items.GroupBy(s => s.Category)) 
{ 
    var expensive = item.Max(x => x.Price); 
    var listWithoutMax = item.Select(x => x.Price != expensive.Price).ToList(); 
} 
1

이 작업을 시도 할 수 있습니다.

 class Item 
     { 
      public string Category { get; set; } 
      public int Price { get; set; } 
     } 
     var prods = new List<Item> 
     { 
      new Item { Category = "A", Price = 100}, 
      new Item { Category = "A", Price = 101}, 
      new Item { Category = "B", Price = 200}, 
      new Item { Category = "B", Price = 201}, 
     }; 

     var data = prods 
      .GroupBy(prod => prod.Category) 
      .Select(p => new Item { Category = p.Key, Price = p.Min(x => x.Price) }) 
      .ToList();