3

I have a 3 tables Dish, Wine, Suggestion. Then the idea is use table suggestion table to put the dish and the wine making one of them suggestion each other. I'm using LINQ, but when one product doesn't have a suggestion he does not add to the json.

var query = (from m in db.Dish
            join t in db.TypeDish on m.idTypeDish equals t.idTypeDish
            join i in db.ImageDish on m.idDish equals i.idDish into g
            join s in db.Suggestion on m.idDish equals s.idDish
            join si in db.ImageWine on s.idWine equals si.idWine into f
            where m.idTypeDish == dish

            select new DishModel()
            {
                Name = m.name,
                CalorificValue = m.calorificValue,
                Price = m.price,
                ShortName = m.shortName,
                Time = m.manufactureTime,
                Description = m.description,
                UrlImageList = g.Select(i => _url + i.Image.urlImage).ToList(),
                BeveragesList = new List<BeverageModel>()
                {
                    new BeverageModel()
                    {
                        Name = s.Wine.name,
                        ShortName =  s.Wine.shortName,
                        Price =  s.Wine.price,
                        Description = s.Wine.description,
                        AlcoholContent = s.Wine.alcoholContent,
                        WineEnum = WineEnum.WhiteWine,
                        Region =  s.Wine.Region.name,
                        WineCaste =  s.Wine.wineCaste,
                        UrlImageList = f.Select(i => _url+i.Image.urlImage).ToList(),
                    }
                }
            }).ToList();


        return query;

Now I have 2 items on DB, and he sends only one because the other don't have a suggestion. The error is on joins probably, but I'm a newbie in Linq.

Thanks.

Arroz
  • 113
  • 1
  • 9
  • 1
    Possible duplicate of [Linq join iquery, how to use defaultifempty](https://stackoverflow.com/questions/19293844/linq-join-iquery-how-to-use-defaultifempty) – Fran Jun 12 '17 at 14:25
  • Try Left Outer Join. See msdn : https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b – jdweng Jun 12 '17 at 14:36
  • 1
    Hey thanks, but now I have a problem he is repeating the data because of the Image Suggestion, I put 2 left outer joins the Suggestion and the Images Of The Suggestion. – Arroz Jun 13 '17 at 11:03

0 Answers0