0

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:

enter image description here

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; }
    }
}
A191919
  • 3,422
  • 7
  • 49
  • 93
  • `but it giving wrong result.` What is it? Sample input? expected output? – Eser Mar 26 '16 at 23:28
  • Correct your code so it runs, right now it doesn't from what you were saying gave an answer on how to do a left join in linq, but your code doesn't compile, show the code that is currently working – konkked Mar 26 '16 at 23:42
  • Is the relationship from Dish => Recipe 1 to 1? Your edit made me confused – konkked Mar 26 '16 at 23:51
  • @konkked,Yes i see confusing thing. – A191919 Mar 26 '16 at 23:53
  • Made modifications to answer, uses slightly less code and to answer your question so you can remove the answer part you put in your question, pretty sure isn't supposed to be in there – konkked Mar 26 '16 at 23:58

1 Answers1

2

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(); 
konkked
  • 3,161
  • 14
  • 19