c# – 如何使用LINQ合并来自多个IEnumerable的结果

c# – 如何使用LINQ合并来自多个IEnumerable的结果,第1张

概述我可以有多个(最多5个)IEnumerable< ResortSupplier>并希望将它们合并为单个IEnumerable< ResortSupplier>按SupplierCode分组.我的对象看起来像这样: 这是我的对象: [Serializable]public class ResortSupplier{ public string SupplierCode { get; se 我可以有多个(最多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> - 1Hyatt Resort  Standard Room    PricingDetail-1  Superior Room             PricingDetail-1Sandals Resort  Standard Room    PricingDetail-1  Delux Room                PricingDetail-1----------------------------------------IEnumerable<Resortsupplier> - 2Hyatt Resort  Standard Room    PricingDetail-2  Superior Room             PricingDetail-2  One bed Room Suit    PricingDetail-2Sandals Resort  Standard Room    PricingDetail-2  Honeymoon Suit                PricingDetail-2----------------------------------------IEnumerable<Resortsupplier> - 3.....----------------------------------------IEnumerable<Resortsupplier> - 4.....----------------------------------------IEnumerable<Resortsupplier> - 5.....----------------------------------------

我的综合结果应包含:

IEnumerable<Resortsupplier> - ConsolIDatedHyatt Resort  Standard Room    PricingDetail-1    PricingDetail-2  Superior Room             PricingDetail-1    PricingDetail-2  bed Room Suit    PricingDetail-2Sandals Resort  Standard Room    PricingDetail-1    PricingDetail-2  Delux Room                PricingDetail-1  Honeymoon Suit                PricingDetail-2----------------------------------------

处理这个问题的最佳方法是什么?我尝试了简单的IEnumerable.Union和GroupBy子句,但没有得到我想要的结果.

解决方法 我想你想要的东西:
// 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()                                          };

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

总结

以上是内存溢出为你收集整理的c# – 如何使用LINQ合并来自多个IEnumerable的结果全部内容,希望文章能够帮你解决c# – 如何使用LINQ合并来自多个IEnumerable的结果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1244750.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存