Click here to Skip to main content
15,885,141 members
Articles / Web Development / ASP.NET

Sample LINQ with Multiple Joins and Selected Columns

Rate me:
Please Sign up or sign in to vote.
4.58/5 (8 votes)
1 Jun 2020CPOL 27.1K   6   4
How to write a LINQ statement with multiple joins
In this post, we will go through a sample that shows how to write a LINQ statement with multiple joins.

If you are like me, I sometimes find it difficult to remember the syntax of LINQ statements because I jump from T-SQL to ASP.NET and C#.

I was searching for a sample that may show how to write a LINQ statement with multiple joins and I had little luck finding it, so finally, after I put my few brain cells in action, I came up with the following:

C#
var dbRegCourses = (from a in db.CourseRegistries
                    join b in db.Courses on a.courseid equals b.id
                    join c in db.aspnet_Users on a.userid equals c.UserId
                    where a.userid == sUserID
                    orderby a.regdate, b.code, b.description,
                            b.instructor, b.date, b.venue
                    select new
                    {
                      a.regdate, b.code, b.description,
                      b.instructor, b.date, b.venue});

if (dbRegCourses.Count() > 0)
{
    ResultLbl.Text = "We found that you are registered to: " +     
                      dbRegCourses.Count().ToString() + " Courses.";
    return;
}

If you notice, here we are joining three tables, using a where statement and then picking and choosing columns from at least two tables.

I also added an if statement bottom to see if I got any rows back from the LINQ Statement and if that's the case, return a message.

Hope this helps somebody out there,
Will

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionOT: Any vs Count Pin
Daniele Rota Nodari3-Jun-20 4:48
Daniele Rota Nodari3-Jun-20 4:48 
Off topic, I know, but....
When checking for existence of records, use Any() instead of Count().

Count might fetch all records, whereas Any should fetch at most one; the query provider is responsible to build the more specific and optimized query it can, and declaring the correct intent should help in this task (e.g.: checking for existence instead of fetching all records).
Moreover, the intent would be more clear to another developer (included the "future you").

In short, with Any vs Count...
- The real intent is more clear.
- The query or instruction would probably consume less resources and execute faster.
QuestionNot LINQ but.. Pin
cphv2-Jun-20 4:26
cphv2-Jun-20 4:26 
QuestionJoining on a null Pin
Member 120782262-Jun-20 3:07
Member 120782262-Jun-20 3:07 
QuestionFluent Syntax Pin
George Swan2-Jun-20 2:24
mveGeorge Swan2-Jun-20 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.