Linq To SQL 加入
Linq To SQL Join
我正在学习 Linq2SQL,我对左外连接有疑问。
在下面的示例中,我相信我正在对问题 table 执行左外连接到最喜欢的问题 table。但是我不相信我的 where 子句是正确的。
因此,如果我对两个 table 执行遗漏连接,我应该如何适当地设置 where 子句?
var myResults = from quest in context.MyQuestions
join favQuest in context.MyFavoriteQuestions on quest.UserFavoriteQuestionId equals favQuest.UserFavoriteQuestionId
join specialQuest in context.Questions on favQuest.QuestionId equals specialQuest.QuestionId into joinedQuestions
from specialQuest in joinedQuestions.DefaultIfEmpty()
where (quest.UserId == userId) &&
( specialQuest.Id == paramId && (!specialQuest.IsBlue || (specialQuest.IsBlue && canViewBlueQuestion)) &&
(!specialQuest.IsRed || (specialQuest.IsRed && canViewRedQuestion))
)
select quest;
对于 LINQ to SQL 上下文,建议这样编写左外连接,因为它实际上会生成 SQL LEFT JOIN:
var myResults = from question in context.MyQuestions
from favoriteQuestion in context.MyFavoriteQuestions
.Where(fc => fc.UserFavoriteQuestionId == question.UserFavoriteQuestionId)
.DefaultIfEmpty()
还建议(以提高易读性)分隔不相关的(和 AND
ed)where 从句:
var myResults = from question in context.MyQuestions
where question.UserId == userId
from favoriteQuestion in context.MyFavoriteQuestions
.Where(fc => fc.UserFavoriteQuestionId == question.UserFavoriteQuestionId)
.DefaultIfEmpty()
from specialQuestion in context.Questions
.Where(sc => sc.QuestionId == favoriteQuestion.QuestionId)
.DefaultIfEmpty()
where specialQuestion.Id == paramId
where !specialQuestion.IsBlue || (specialQuestion.IsBlue && canViewBlueQuestion)
where !specialQuestion.IsRed || (specialQuestion.IsRed && canViewRedQuestion)
select question;
我正在学习 Linq2SQL,我对左外连接有疑问。 在下面的示例中,我相信我正在对问题 table 执行左外连接到最喜欢的问题 table。但是我不相信我的 where 子句是正确的。 因此,如果我对两个 table 执行遗漏连接,我应该如何适当地设置 where 子句?
var myResults = from quest in context.MyQuestions
join favQuest in context.MyFavoriteQuestions on quest.UserFavoriteQuestionId equals favQuest.UserFavoriteQuestionId
join specialQuest in context.Questions on favQuest.QuestionId equals specialQuest.QuestionId into joinedQuestions
from specialQuest in joinedQuestions.DefaultIfEmpty()
where (quest.UserId == userId) &&
( specialQuest.Id == paramId && (!specialQuest.IsBlue || (specialQuest.IsBlue && canViewBlueQuestion)) &&
(!specialQuest.IsRed || (specialQuest.IsRed && canViewRedQuestion))
)
select quest;
对于 LINQ to SQL 上下文,建议这样编写左外连接,因为它实际上会生成 SQL LEFT JOIN:
var myResults = from question in context.MyQuestions
from favoriteQuestion in context.MyFavoriteQuestions
.Where(fc => fc.UserFavoriteQuestionId == question.UserFavoriteQuestionId)
.DefaultIfEmpty()
还建议(以提高易读性)分隔不相关的(和 AND
ed)where 从句:
var myResults = from question in context.MyQuestions
where question.UserId == userId
from favoriteQuestion in context.MyFavoriteQuestions
.Where(fc => fc.UserFavoriteQuestionId == question.UserFavoriteQuestionId)
.DefaultIfEmpty()
from specialQuestion in context.Questions
.Where(sc => sc.QuestionId == favoriteQuestion.QuestionId)
.DefaultIfEmpty()
where specialQuestion.Id == paramId
where !specialQuestion.IsBlue || (specialQuestion.IsBlue && canViewBlueQuestion)
where !specialQuestion.IsRed || (specialQuestion.IsRed && canViewRedQuestion)
select question;