Linq join of 3 tables

Lloyd Sheen 1,486 Reputation points
2024-04-23T20:58:23.7866667+00:00

I have three tables with same fields. I can join 2 tables with following Linq.

 List<CommonPoints> lista = poso.assists.ToList();

 List<CommonPoints> listp = poso.points.ToList();

 List<CommonPoints> listg=poso.goals.ToList();

 var result = from p in listp

          join a in lista

             on p.id equals a.id

          select new Combineded(p.id, p.value, a.value,p.firstName.TheDefault,p.lastName.TheDefault );

I have third table (listg), how could I join the third table with other two or am I going about this the wrong way?

Developer technologies | .NET | .NET MAUI
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-04-24T02:01:48.76+00:00

    Hello,

    You can use LINQ query to join the third table (listg) in a similar way to how you joined the first two tables like following code.

    var result = from p in listp
                 join a in lista on p.id equals a.id
                 join g in listg on p.id equals g.id
                 select new Combined(p.id, p.value, a.value, g.value, p.firstName.TheDefault, p.lastName.TheDefault);
    

    As note:Please make sure all three tables (listp, lista, and listg) have the same field structure and you want to join them based on the id field.

    Best Regards,

    Leon Lu


    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.