EF Core by Jarvis

Subquery on Two Columns in LINQ

In EF Core, joining a subquery with two columns (INNER JOIN table) can be achieved using LINQ syntax. Below is an example of how to do this:

var query = from user in context.Users
            join post in context.Posts
            on new { UserId = user.Id, IsPublished = true }
            equals new { post.UserId, IsPublished = true }
            select new
            {
                user.Username,
                post.Title
            };

In this example

  • The LINQ query joins the Users and Posts tables on two columns (UserId and a condition IsPublished) using the equals keyword.
  • In a LINQ join operation, you need to match corresponding columns or properties from the joined tables/entities. In the case of UserId = user.Id, you are matching the UserId column from the Post entity with the Id column from the User entity.

Additional reading at EF Core Join Query - TekTutorialsHub

Comments

Leave a Comment

All fields are required. Your email address will not be published.