Как объединить результаты из нескольких IEnumerable <T>, используя LINQ - PullRequest
7 голосов
/ 26 апреля 2011

Я мог бы иметь несколько (до 5) IEnumerable<ResortSupplier> и хотел бы объединить их в одну IEnumerable<ResortSupplier> группировку по SupplierCode. Мои объекты выглядят так:

Вот мои объекты:

[Serializable]
public class ResortSupplier
{
    public string SupplierCode { get; set; }
    public IList<Product> ResortProducts { get; set; }
}

[Serializable]
public class Product
{
    public string Code { get; set; }
    //Other fields... 
    public IList<PerPricing> PricingDetail { get; set; }
}

[Serializable]
public class PerPricing
{
    //Other fields..
    public decimal? Price { get; set; }
    public Error PricingError { get; set; }
}

Данные в нескольких IEnumerable могут выглядеть следующим образом:

----------------------------------------
IEnumerable<ResortSupplier> - 1

Hyatt Resort
  Standard Room
    PricingDetail-1
  Superior Room         
    PricingDetail-1

Sandals Resort
  Standard Room
    PricingDetail-1
  Delux Room            
    PricingDetail-1

----------------------------------------
IEnumerable<ResortSupplier> - 2

Hyatt Resort
  Standard Room
    PricingDetail-2
  Superior Room         
    PricingDetail-2
  One Bed Room Suit
    PricingDetail-2

Sandals Resort
  Standard Room
    PricingDetail-2
  Honeymoon Suit            
    PricingDetail-2
----------------------------------------
IEnumerable<ResortSupplier> - 3
.....
----------------------------------------
IEnumerable<ResortSupplier> - 4
.....
----------------------------------------
IEnumerable<ResortSupplier> - 5
.....
----------------------------------------

Мой сводный результат должен содержать:

IEnumerable<ResortSupplier> - Consolidated

Hyatt Resort
  Standard Room
    PricingDetail-1
    PricingDetail-2
  Superior Room         
    PricingDetail-1
    PricingDetail-2
  Bed Room Suit
    PricingDetail-2

Sandals Resort
  Standard Room
    PricingDetail-1
    PricingDetail-2
  Delux Room            
    PricingDetail-1
  Honeymoon Suit            
    PricingDetail-2

----------------------------------------

Какой лучший способ справиться с этим? Я попробовал простые предложения IEnumerable.Union и GroupBy, но не получил желаемых результатов.

Ответы [ 2 ]

3 голосов
/ 26 апреля 2011

Если я вас понимаю, вы должны использовать Concat, чтобы собрать их в одну большую коллекцию, затем вы можете группировать, как вам захочется

IEnumerable<ResortSupplier> master = 
    result1.Concat(result2).Concat(result3); //etc

Тогда группируйте, как обычно:

var resultGrouped = master.GroupBy(rs => rs.SupplierCode);
2 голосов
/ 26 апреля 2011

Я думаю, вы хотите что-то вроде:

// You can construct this with an array if it isn't already in this form.
IEnumerable<IEnumerable<ResortSupplier>> supplierLists = ...

var query = from suppliers in supplierLists
            from supplier in suppliers
            group supplier by supplier.SupplierCode into g

            let products = g.SelectMany(s => s.ResortProducts)
                            .GroupBy(p => p.Code)
                            .Select(prodGroup => new Product
                                    {
                                       Code = prodGroup.Key,
                                       PricingDetail = prodGroup
                                                       .SelectMany(p => p.PricingDetail)
                                                       .ToList()
                                    })

            select new ResortSupplier
            {
                SupplierCode  = g.Key, 
                ResortProducts = products.ToList()                              
            };

Это действительно ужасно выглядящий запрос. Я настоятельно рекомендую разделять различные компоненты запроса на отдельные (хорошо именованные) методы.

...