How to consolidate results from multiple IEnumerable<T> using LINQ
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 开发者_如何学Python
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.
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);
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.
精彩评论