title: LINQ: Convert DataTable to User Defined Class containing array
body: <p>I have this object structure</p>
  
  <pre><code>public class Product
  {
  public int Id {get;set;}
  public string DisplayName{get;set;}
  public Ingredient[] Ingredents{get;set;}
  }
  
  public class Ingredient
  {
   public int id{get;set;}
   public string Name{get;set;}
  }
  </code></pre>
  
  <p>I have the datatable with the below result set</p>
  
  <pre><code>    Id | DisplayName  |  IngredientId | IngName
  -------------+-------------------------+------------------------
      1  | Prod1         |   1           |  Ing1
      1  | Prod1         |   2           |  Ing2
      1  | Prod1         |   3           |  Ing3
      2  | Prod2         |   1           |  Ing1
      2  | Prod2         |   2           |  Ing2
      3  | Prod3         |   1           |  Ing1
      3  | Prod3         |   2           |  Ing2
  </code></pre>
  
  <p>I need to have LINQ query which would give me list of Products as below:
  // pseudo code which describes structure:</p>
  
  <pre><code>resultProductList =
  {
    new Product() { Id = 1
                  , DisplayName = "Prod1"
                  , Ingredients = {
                                   new Ingredient{ Id = 1, Name = "Ing1" } 
                                  , new Ingredient{ Id = 2, Name = "Ing2" } 
                                  , new Ingredient{ Id = 3, Name = "Ing3" } 
                                  }
   , new Product() { Id = 2
                  , DisplayName = "Prod2"
                  , Ingredients = {
                                   new Ingredient{ Id = 1, Name = "Ing1" } 
                                  , new Ingredient{ Id = 2, Name = "Ing2" } 
                                  }
   , new Product() { Id = 3
                  , DisplayName = "Prod3"
                  , Ingredients = {
                                   new Ingredient{ Id = 1, Name = "Ing1" } 
                                  , new Ingredient{ Id = 2, Name = "Ing2" } 
                                  }
  
  }
  </code></pre>
  
  <p>I refered <a href="https://stackoverflow.com/questions/3070153/linq-converting-flat-structure-to-hierarchical">this</a> but I am unable to get the solution.</p>
  
  <pre><code>List&lt;Product&gt; lstContracttype = dsGetProducts.Tables[0]
                                        .AsEnumerable()
                                        .Select(x =&gt; new Product
                                        {
                                            Id= Convert.ToInt32(x[0]),
                                            DisplayName= Convert.ToString(x[1])
                                        })
                                        .GroupBy(y =&gt; y.id).ToList();
  </code></pre>
  
  <p>Could anyone please suggest how could I write LINQ query for get this result?</p>
  
  <p><strong>UPDATE:</strong>
  I added one more sub-class and added it to main Product class. See below:</p>
  
  <pre><code>public class Product
      {
      public int Id {get;set;}
      public string DisplayName{get;set;}
      public Ingredient[] Ingredents{get;set;}
      public Price MyPrice{get;set;}
      }
  public class Price
  {
      public int min {get;set;}
      public int max {get;set;}
      public int defaultPrice {get;set;}
  }
  </code></pre>
  
  <p>I modified the LINQ query as follows and am getting the results.
  My question is in below LINQ query, should I write <code>.First&lt;Price&gt;</code> or <code>.FirstOrDefault&lt;Price&gt;</code>? 
  Is this proper way to handle this condition or is there any other way?</p>
  
  <pre><code>var lstProducts = dsGetProducts.Tables[0].AsEnumerable().Cast&lt;DataRow&gt;()
                    .GroupBy(r =&gt; new { Id = r["Id"], 
                                        DisplayName = r["DisplayName"], 
                                        priceMax = r["priceMax"],
                                        priceMin = r["priceMin"],
                                        priceDefault = r["priceDefault"]
                                      })
                    .Select(g =&gt; new Product()
                    {
                       Id = (int)g.Key.Id,
                       DisplayName = (string)g.Key.DisplayName,
                       Ingredients = g.Select(r =&gt; new Ingredient()
                       {
                          id = (int)r["IngredientId"],
                          Name = (string)r["IngName"]
                       }).ToArray(),
                       MyPrice =  g.Select(r =&gt; new Price()
                       {
                          min= (int)r["minPrice"],
                          max= (int)r["maxPrice"],
                          defaultPrice= (int)r["priceDefault"],
                       }).FirstOrDefault&lt;Price&gt;()
  </code></pre>