none
Multiple join Linq C# RRS feed

  • Question

  • Hello!

    Im trying to do a multiple join with linq, but VS tell me im doing wrong. What's my problem? here is an example of the code

    var model = from r in _db.Product
                            join img in _db.Picture on r.ImageId equals img.Id
                            join comp in _db.Company on r.CompanyId equals comp.Id
                            select new { Name = r.Name, Company = comp.Name, Price = r.Price, Picture = img.Picture };

    Sunday, March 13, 2016 10:07 PM

Answers

  • Hi Bramdont,

    Based on your code, I create similar demo (code as below), the code seems ok, could you please provide a bit more information (such as error message, every table’s model code), I can better find what cause this issue and find a solution to solve it.

    using (var db = new TestEntities())
                {
                    var query = from p in db.Products
                                join img in db.Pictures on p.ImageId equals img.PictureId
                                join c in db.Companies on p.CompanyId_ equals c.CompanyId
                                select new
                                {
                                    Name = p.Name,
                                    Company = c.Name,
                                    Price = p.Price,
                                    Picture = img.Picture1
                                };
                    foreach (var item in query)
                    {
                        Console.WriteLine("{0} --- {1}", item.Name, item.Company);
                    }
                    Console.ReadKey();
                }

    Best regards,

    Cole Wu


    Monday, March 14, 2016 4:19 AM
    Moderator