LINQ2SQL: Joining Multiple Tables

I'm not so good at linq2sql or even sql, but everything comes with a little training.

My question: I have 3 tables: Projects, Folders and Tasks.

I need to request me to join these 3 tables.

I tried a little:

public ActionResult Details(string id) {
        var user = GetProfile().Id;
        var p = _db.Projects.Where(x => Convert.ToString(x.ProjectId) == id && x.UserId == user).SingleOrDefault();
        var f = _db.Folders.Where(x => x.ProjectId == p.ProjectId).ToList();
        return View(f);
    }

This works great and I get folders related to the project. Now I want, in the same query, tasks that are related to folders and the project.

So, at the end, I get this scenario: Click on the project name, send the identifier, take the identifier and present the folders related to the project that I clicked on and present the tasks on the same site as the folders.

+3
source share
2 answers

, . linq to sql, . ProjectId, Folders "ProjectId", . "FolderId", "FolderId", . , :

var result = (from projects in _db.Projects
                      join folders in _db.Folders
                      on projects.ProjectId equals folders.ProjectId
                      join tasks in _db.Tasks
                      on folders.FolderId equals tasks.FolderId
                      where projects.Id.ToString() == id
                      && projects.UserId == user
                      select projects).ToList();

, .

+7

SQL Linq, , , SQL- LINQ, ,

Linqer

+1

Source: https://habr.com/ru/post/1784149/


All Articles