Aggregating data with query for ASP MVC 3 controller

kraiachei

New Member
I have three tables:\[quote\] PackageName: PackageNameID,PackageName Food: FoodID,Food PackageContent: PackageContentID, PackageNameID, FoodID, Qty\[/quote\]What I'm trying to do is display something like this:\[quote\]
  • PackageName (Food=Qty, Food=Qty) Edit | Details | Delete
  • A (Onions=4,Peppers=3) Edit | Details | Delete
  • B (Rolls=2,Onions=2) Edit | Details | Delete
\[/quote\]Here is my view model:\[code\]public class PackageViewModel{ public int ID { set;get;} public string Name { set;get;} public List<FoodItem> FoodItems { set;get;} public PackageViewModel() { FoodItems=new List<FoodItem>(); }}public class FoodItem{ public string FoodName { set;get;} public int Quantity { set;get;}}\[/code\]Here is my view:\[code\]@model List<PackageViewModel>@foreach(var item in Model){ <tr> <td>@item.Name</td> <td> foreach(var food in item.FoodItems) { @food.FoodName - @food.Quantity } </td> <td> @Html.ActionLink("Edit","Edit",new {@id=item.ID}) </td> </tr>}\[/code\]This is the query I am using in my controller:\[code\]List< PackageViewModel > listVM=new List< PackageViewModel >();listVM = from pn in db.PackageNames from pc in db.PackageContents .Where(p => p.PackageNameID == pn.PackageNameID).DefaultIfEmpty() from f in db.Foods .Where(f => f.FoodID == pc.FoodID).DefaultIfEmpty() select new PackageViewModel { PackageNameID = pc.PackageNameID, PackageName = pn.PackageName, FoodItems = f.Food.ToList() }; return View(listVM); \[/code\]But, I get the following error: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'Further, the select line isn't right anyway....I need to assign both the name of the food ("Food" from the "Food" table) and the quantity of this food item ("Qty" from the "PackageContent" table) to the "FoodItems" list in the model. How do I do this?Thanks very much!!
 
Back
Top