Linq join of 3 tables

Lloyd Sheen 1,386 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?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,897 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,274 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    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