简体   繁体   中英

Write the equivalent SQL Select statement with 2 Left Join in LINQ

I need to convert the following SQL select statement below to its LINQ equivalent.

SQL STATEMENT:

SELECT MainTitles.Title,
    SubTitles.Title,
    ContentResources.ResourceContent,
    SubTitles.MainTitleId,
    SubTitles.SubTitleId
FROM ContentResources
LEFT JOIN MainTitles on ContentResources.MainTitleId = MainTitles.MainTitleId
LEFT JOIN SubTitles on ContentResources.ContentResourceId = SubTitles.ContentResourceId

TABLE SAMPLES:

Table Structure with Sample Data

RESULT OF THE STATEMENT:

The desired result using SQL is shown in the screenshot below.

[ 上面SQL语句的查询结果

WHAT I HAVE TRIED IN LINQ

var resources = (from c in _context.ContentResources
                 join m in _context.MainTitles
                 on c.MainTitleId equals m.MainTitleId into ResultTable1
                 from result in ResultTable1.DefaultIfEmpty()

                 join s in _context.SubTitles
                 on c.ContentResourceId equals s.SubTitleId
                 on c.ContentResourceId equals s.SubTitleId into ResultTable2

                 from result2 in ResultTable2.DefaultIfEmpty()
                 select new
                 {
                     c.ResourceContent,
                     c.ContentResourceId,
                     c.MainTitleId,
                     result.Title,
                     s.SubTitle
                  }).ToList();

Please help me. I have seen for 1 Left Join and that is how I was able to try what I have above. I even tried the free version of LINQPad7 but I have no idea how to convert the SQL to LINQ using it. Thanks in advance.

I would use Entity Framework's entity loading.

https://docs.microsoft.com/en-us/ef/ef6/querying/related-data

Update your models to include virtuals:

public class ContentResource
{
    public virtual MainTitle MainTitle { get; set; }
    public virtual SubTitle SubTitle { get; set; }
}

Then you could create a query like this:

var resources = _context.ContentResource
    .Include(x => x.MainTitle)
    .Include(x => x.SubTitle)
    .Select(x => new
    {
        ResourceContent = x.ResourceContent,
        ContentResourceId = x.ContentResourceId,
        MainTitleId = x.MainTitle.MainTitleId,
        MainTitle = x.MainTitle.Title,
        SubTitle = x.SubTitle.SubTitle
    })
    .ToList();

Try the following. It not tested but this is how multiple joins are done in LINQ.

var resources = from c in _context.ContentResources 
             join m in _context.MainTitles on c.MainTitleId equals m.MainTitleId
             join s in Context.SubTitles on m.ContentResourceId equals s.ContentResourceId 
             select new {
                 c.ResourceContent,
                 c.ContentResourceId,
                 m.MainTitleId,
                 s.Title,
                 s.SubTitle }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM