Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am struggling to call 3 tables in linq, can anyone help on this given logic. I am getting properties are not exist in current context.

What I have tried:

var list1 = (from w in db.Weeks  
             from p in db.ProductionDays on w.WeekId = p.WeekId  // Error here
             from m in db.Models on p.ProductionDate =  m.CreatedDate // Error here 
             where  
             (myModels.Contains(m.Name.ToString().ToUpper()))  
             select  
             {  
              w.Year,  
              w.WeekNum,  
              p.ProductionDate,  
              p.DayOfWeek,  
              m.Name,  
              m.Code,  
              m.InActive  
             }).ToList(); 
Posted
Updated 28-Apr-21 21:49pm
Comments
[no name] 28-Apr-21 15:19pm    
Do 2 "sets" at a time ... if it makes it easier to get your head around. (A "set" being a table, or the result of a "2 table" join).

(How do you think it gets done "under the covers"?)
George Swan 29-Apr-21 3:39am    
Looks like you assigning w.WeelId to p.WeelId by using '=' when you should be using the equality operator '=='

1 solution

To join sets in LINQ, you use the join keyword, not the from keyword.
join clause - C# Reference | Microsoft Docs[^]
C#
from w in db.Weeks  
join p in db.ProductionDays on w.WeekId equals p.WeekId
join m in db.Models on p.ProductionDate equals m.CreatedDate
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900