Reputation: 3452
How to write Description
from Recipe
in recipe=
.
I tried to use join r in Recipe on d.DishID equals r.DishID
but it giving wrong result. It erase one item in result1.
Exptected output:
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
List<Component> Component = new List<Component>();
List<Dish> Dish = new List<Dish>();
List<Recipe> Recipe = new List<Recipe>();
Dish.Add(new Dish { DishID = 9, CategoryID = 6, DishName = "Pork" });
Dish.Add(new Dish { DishID = 10, CategoryID = 6, DishName = "Beef" });
Component.Add(new Component { ComponentID = 1, DishID = 9, AmountID = "1", NameID = "1" });
Recipe.Add(new Recipe { DishID = 9, RecipeID = 0, Description = "Test" });
var result1 = (
from d in Dish
//join r in Recipe on d.DishID equals r.DishID
join c in Component on d.DishID equals c.DishID into items
select new Item { DishID = d.DishID, components = items.ToList() recipe=}
).ToList();
}
}
public class Item
{
public int DishID { get; set; }
public string DishName { get; set; }
public string recipe { get; set; }
public List<Component> components { get; set; }
}
public partial class Component
{
public int ComponentID { get; set; }
public int DishID { get; set; }
public string AmountID { get; set; }
public string NameID { get; set; }
}
public partial class Dish
{
public int DishID { get; set; }
public int CategoryID { get; set; }
public string DishName { get; set; }
}
public partial class Recipe
{
public int RecipeID { get; set; }
public int DishID { get; set; }
public string Description { get; set; }
}
}
Upvotes: 0
Views: 65
Reputation: 3231
You want to do a left join
To do this you need to do some extra stuff in linq, not sure why it isnt support natively but it isnt.
var result1 = (
from d in Dish
join c in Component on d.DishID equals c.DishID into items
join r in Recipe on d.DishID equals r.DishID into recipes
select new Item {
DishID = d.DishID,
components = items.DefaultIfEmpty()
.Where(a=>a!=null)
.ToArray(),
recipe = recipes.DefaultIfEmpty()
.Where(a=>a!=null)
.Select(a=>a.Description)
.FirstOrDefault()
}).ToList();
Upvotes: 2