如何合并多个 IEnumerable的结果使用LINQ

发布于 2024-11-03 13:31:22 字数 2166 浏览 0 评论 0原文

我可以有多个(最多 5 个)IEnumerable,并希望将它们合并为按供应商代码分组的单个 IEnumerable。我的对象如下所示:

这是我的对象:

[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 子句,但没有得到我想要的结果。

I could have multiple (upto 5) IEnumerable<ResortSupplier> and would like to consolidate them into single IEnumerable<ResortSupplier> grouping by SupplierCode. My objects looks like this:

Here are my objects:

[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; }
}

Data in multiple IEnumerable could look like this:

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

My consolidated result should contain:

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

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

What is the best way to handle this? I tried simple IEnumerable.Union and GroupBy clauses but doesn't get me the results I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲世九天 2024-11-10 13:31:22

如果我理解你的意思,你应该使用 Concat 将它们放入一个大集合中,然后你可以按照你想要的方式进行分组

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

然后像平常一样进行分组:

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

If I'm understanding you, you should use Concat to get them into one big collection, then you can group however you want

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

Then group like you normally would:

var resultGrouped = master.GroupBy(rs => rs.SupplierCode);
神妖 2024-11-10 13:31:22

我想你想要这样的东西:

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

但这确实是一个看起来很糟糕的查询。我强烈建议将查询的不同组成部分分解为单独的(命名良好的)方法。

I think you want something like:

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

That's a really horrible-looking query though. I strongly recommend factoring out the different components of the query into separate (well-named) methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文