如何合并多个 IEnumerable的结果使用LINQ
我可以有多个(最多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解你的意思,你应该使用 Concat 将它们放入一个大集合中,然后你可以按照你想要的方式进行分组
然后像平常一样进行分组:
If I'm understanding you, you should use Concat to get them into one big collection, then you can group however you want
Then group like you normally would:
我想你想要这样的东西:
但这确实是一个看起来很糟糕的查询。我强烈建议将查询的不同组成部分分解为单独的(命名良好的)方法。
I think you want something like:
That's a really horrible-looking query though. I strongly recommend factoring out the different components of the query into separate (well-named) methods.