title: Bind a Linq Lambda Select Many from Controller to view in MVC4? What are the Best ways to return in view
body: <p>I am new to MVC by Linq. When I am working with Console application its working fine.<br>
  When i shift to Mvc Architecture, passing the values from controller to view i am getting error as</p>
  
  <p><strong>The model item passed into the dictionary is of type 'System.Collections.Generic.List<code>1[&lt;&gt;f__AnonymousType2</code>2[System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication2.Models.Teams]'.</strong></p>
  
  <p>I know i am passing Objects as stringName,String and its expecting 'System.Collections.Generic.IEnumerable how to resolve this kind of issues</p>
  
  <p>This Scenario happen for  Multiple groupby and Selectmany in Linq .how to pass the value and get into bind for View . what is the Best approach.</p>
  
  <p>i have put a foreach in  Controller and return or else do the foreach iterations in view for selectMany,Groupby,Lookup etc.</p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>public Class Product
  {
      public string CategoryName {get; set; }
      public int Productid { get; set; }
      public List&lt;String&gt; Productname { get; set; }
  
      public static List&lt;Product&gt; GetAllProductName()
      {
          var ListofProducts = new List&lt;Product&gt;
          {
              new Product { CategoryName = "Choclateid", Productid = 1, Productname = new List&lt;string&gt; { "DairyMilkshots", "DairyMilk Silk", "DairyMilkFruitsNuts" }},
              new Product { CategoryName = "Vegetables", Productid = 2, Productname = new List&lt;string&gt; { "Tomoto", "Pototo", "Onion"}},
              new Product { CategoryName  = "Fruits", Productid = 3, Productname = new List&lt;string&gt; {"Apple", "Orange", "Strawberry"}},
          };
          return ListofProducts;
      }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public ActionResult Home()
  {
       var ny = Product.GetAllTeamMembers()
                   .SelectMany(x =&gt; x.Productname, (category, withProductlist) =&gt; 
                                                    new { cat = category.CategoryName, PN = Productname })
                   .ToList();
  
       return View(ny);
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model IEnumerable&lt; MvcApplication2.Models.Product&gt;
  
  @{
      ViewBag.Title = "Selectmany";
  }
  
  &lt;h2&gt;@ViewBag.Message&lt;/h2&gt;
  &lt;p&gt;
      To learn more about ASP.NET MVC visit &lt;a href="http://asp.net/mvc" title="ASP.NET MVC Website"&gt;http://asp.net/mvc&lt;/a&gt;.
  &lt;/p&gt;
  @{  
      foreach(var k in Model)
      {
           @K.CategoryName&lt;br/&gt;
           @k.Productname&lt;br/&gt;
       }                    
   }
  </code></pre>
  
  <p><strong>Console</strong></p>
  
  <pre><code>var ny = Product.GetAllTeamMembers().SelectMany(x =&gt; x.Productname,
                                                              (category, withProductlist) =&gt;
                                                               new { cat = category.CategoryName, PN = Productname };
  
  foreach(var n in ny)
  {
     Console.WriteLine(n.cat + "\t" + "-" + n.PN);
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <blockquote>
    <p>Choclate -DairyMilkshots DairyMilk Silk DairyMilkFruitsNuts<br />
    Vegetables - Tomoto,Pototo,Onion<br />
    etc</p>
  </blockquote>