Hi @Lloyd Sheen , Welcome to Microsoft Q&A,
To perform a left outer join with more than two tables in LINQ, you can use multiple join
clauses combined with into
and DefaultIfEmpty()
method. Here's how you can modify your code to join three tables:
var resultx = from p in listp
join g in listg on p.id equals g.id into pgJoin
from pg in pgJoin.DefaultIfEmpty()
join pm in listpm on p.id equals pm.id into pgpmJoin
from pgpm in pgpmJoin.DefaultIfEmpty()
select new Combineded(p.id, p.value, 0, pg?.value ?? 0, pgpm?.value ?? 0, 0, p.firstName.TheDefault, p.lastName.TheDefault);
This query first joins listp
and listg
tables, then joins the result with listpm
table.
You can keep extending this pattern to include more tables:
var resultx = from p in listp
join g in listg on p.id equals g.id into pgJoin
from pg in pgJoin.DefaultIfEmpty()
join pm in listpm on p.id equals pm.id into pgpmJoin
from pgpm in pgpmJoin.DefaultIfEmpty()
join ppg in listppg on p.id equals ppg.id into pgpmppgJoin
from pgpmppg in pgpmppgJoin.DefaultIfEmpty()
join plmn in listplmn on p.id equals plmn.id into pgpmppgplmnJoin
from pgpmppgplmn in pgpmppgplmnJoin.DefaultIfEmpty()
select new Combineded(p.id, p.value, 0, pg?.value ?? 0, pgpm?.value ?? 0, pgpmppg?.value ?? 0, pgpmppgplmn?.value ?? 0, p.firstName.TheDefault, p.lastName.TheDefault);
This query adds a join with the listppg
table, and then another join with the listplmn
table.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.