How to consolidate results from multiple IEnumerable <T> using LINQ

I could have several (up to 5) IEnumerable<ResortSupplier> and would like to combine them into a single IEnumerable<ResortSupplier> grouping by OrderCode. My objects look 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 IEnumerables may 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 the simple articles IEnumerable.Union and GroupBy but didn't get the results I want.

+7
source share
2 answers

I think you need 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() }; 

This is a really terrible request. I highly recommend decomposing the various components of the request into separate (well-named) methods.

+2
source

If I understand you, you must use Concat to get them into one big collection, then you can group, however,

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

Then the group, as usual, will:

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

All Articles