How to combine 3 tables using entity framework and LINQ?

Using a cartesian product in LINQ and Entity framework to combine 3 tables

  • before I return "set.select" I would like to include fields from another table but I canot join this table because it has no fields in common with the other two tables. How may I adjust my code below to achieve this? Iam using vs2012 sql and in MVC c# var set = (from m in managers from t in context.tblCompany join tsc in context.tblStyling on t.ccID equals tsc.ccID select new { tsc.ccID,LogoIcon = tsc.Icon , tsc.style1, tsc.style2, t.Desc }) .ToList(); return set.Select(c => new Settings(c.ccID, c.style1, c.style2, c.Desc, c.LogoIcon , m.firstName , m.lastName));

  • Answer:

    Okay, it looks like you want to join context.tblCompany and context.tblStyle and get the cross product of the resulting set and managers. If that is correct, then you are already there. You just need to include the fields from manager that you want in your select statement: var set = (from m in managers from t in context.tblCompany join tsc in context.tblStyling on t.ccID equals tsc.ccID select new { tsc.ccID, LogoIcon = tsc.Icon, tsc.style1, tsc.style2, t.Desc, m.firstName, m.lastName }) .ToList(); return set;

anchor at Stack Overflow Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.