Linq to sql 如何在Linq to SQL中编写此SQL查询
我想知道如何在Linq中编写以下SQL查询。我试过了,但运气不好Linq to sql 如何在Linq to SQL中编写此SQL查询,linq-to-sql,Linq To Sql,我想知道如何在Linq中编写以下SQL查询。我试过了,但运气不好 /*Variable*/ topicId = '4a31c2ee-48af-4b87-b300-112acb822ad0' select * from Contents c left join ContentToTopicRelations ctr on ctr.ContentId = c.ContentId and ctr.TopicId = topidId where ctr.ContentId is null 基本上,我
/*Variable*/
topicId = '4a31c2ee-48af-4b87-b300-112acb822ad0'
select * from Contents c
left join ContentToTopicRelations ctr on ctr.ContentId = c.ContentId and ctr.TopicId = topidId
where ctr.ContentId is null
基本上,我希望获取特定主题ID的ContentToTopicRelations表中未包含的所有内容
var结果=内容中的c
其中c.TopicId=topicvariable
选择c topicId='4a31c2ee-48af-4b87-b300-112acb822ad0'
可查询查询=
从dataContext.Contents中的c
哪里c、 ContentToTopicRelations.Any(ctr=>ctr.TopicId==TopicId)
选择c;
它与从不存在的内容中选择*相同(…)
。在一般情况下,它比左连接和检查null要好(这取决于表的统计信息,但..),因为它将(在一般情况下)提供半左连接,而不是左连接(在执行计划中)
对于left join本身,请使用如下代码(但我建议使用为您的任务生成不存在的代码):
topicId = '4a31c2ee-48af-4b87-b300-112acb822ad0'
IQueryable<Content> query =
from c in dataContext.Contents
where !c.ContentToTopicRelations.Any(ctr => ctr.TopicId == topicId)
select c;
dataContext.Contents.Where(c =>
!dataContext.ContentToTopicRelations.Any(ctr =>
ctr.ContantId == c.ContentId &&
ctr.TopicId == topicId))
from c in dataContext.Contents
join tempCTR in dataContext.ContentToTopicRelations
on new { c.ContentId, topicId) equals new { tempCTR.ContentId, tempCTR.TopicId }
into tempCTRCollection
from ctr in tempCTRCollection.DefaultIfEmpty()
where ctr == null
select c