Linq中向实体左联接的Sql查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linq中向实体左联接的Sql查询相关的知识,希望对你有一定的参考价值。
我在SQL
查询以下
Select LC.*,LP.[LandingPageName] from [LandingPageCompanies] LC
Left join [LandingPageContent] LP on LP.SubCategoryID=LC.SubCategoryID
where LC.[CategoryID]=17
而且我需要将其转换为LINQ to entity
。
我已经尝试过以下查询,但它的工作方式为Inner join
var data = (from t1 in dbSavingContainer.LandingPageCompanies
join t2 in dbSavingContainer.LandingPageContents on t1.SubCategoryID equals t2.SubCategoryID
where t1.CategoryID == CategoryID
select new
{
CategoryID = t1.CategoryID,
CompanyID = t1.CompanyID,
CompanyLink = t1.CompanyLink,
CompanyLogo = t1.CompanyLogo,
CompanyName = t1.CompanyName,
SubCategoryID = t1.SubCategoryID,
LandingPageName = t2.LandingPageName
}).ToList();
我缺少的地方。 ?
答案
谢谢..我刚刚使用下面的查询解决了它;)
var data = (from t1 in dbSavingContainer.LandingPageCompanies
join t2 in dbSavingContainer.LandingPageContents on t1.SubCategoryID equals t2.SubCategoryID
into x from y in x.DefaultIfEmpty()
where t1.CategoryID == CategoryID
select new
{
CategoryID = t1.CategoryID,
CompanyID = t1.CompanyID,
CompanyLink = t1.CompanyLink,
CompanyLogo = t1.CompanyLogo,
CompanyName = t1.CompanyName,
SubCategoryID = t1.SubCategoryID,
LandingPageName = y.LandingPageName
}).ToList();
以上是关于Linq中向实体左联接的Sql查询的主要内容,如果未能解决你的问题,请参考以下文章
Linq to SQL 使用 Lambda 语法进行左外连接并在 2 列上连接(复合连接键)