如何使用LINQ </t>合并来自多个IEnumerable <t>的结果

时间:2011-04-26 14:34:00

标签: c# linq ienumerable

我可以拥有多个(最多5个)IEnumerable<ResortSupplier>,并希望通过SupplierCode将它们合并为单个IEnumerable<ResortSupplier>分组。我的对象看起来像这样:

以下是我的目标:

[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 个答案:

答案 0 :(得分:3)

如果我理解你,你应该使用Concat将它们变成一个大集合,然后你可以按照你想要的分组

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

然后像你一样分组:

var resultGrouped = master.GroupBy(rs => rs.SupplierCode);

答案 1 :(得分:2)

我想你想要这样的东西:

// 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()                              
            };

但这是一个非常可怕的查询。我强烈建议将查询的不同组件分解为单独的(命名良好的)方法。