Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 906
Alex Avatar asked Apr 26 '11 14:04

Alex


1 Answers

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);
like image 177
Adam Rackis Avatar answered Sep 29 '22 02:09

Adam Rackis