这可以实现这个SQL查询到linq

时间:2013-06-26 07:55:20

标签: sql-server linq tsql sql-to-linq-conversion

我在连接linq中的表时遇到问题,因为我正在检查模板集表中是否存在templateSetId

查询:

select 
    vc.RowID, Choice_Text, VCE.[Goto] 
from 
    ropes.Validation_Choices vc
left outer join 
    ropes.Validation_Condition_Expression VCE 
      on VCE.ChoiceID = vc.RowID and TemplateSetID in 
                                     (select RowID 
                                      from ropes.Validation_Templates_Set 
                                      where TemplateID = 66)
where 
   vc.QuestionID = 81 

此查询返回3行。

移动templatesetid的过滤器会给我一个错误“操作员”&&'不能应用于'int?'类型的操作数和'bool'“ 我能做的最好的就是这个

from tc in _context.ValidationChoice
     join vce in _context.ValidationConditionExpression
     on tc.ChoiceId equals (int)vce.ChoiceID into lfc
     from lf in lfc.DefaultIfEmpty()
     where  tc.QuestionID == questionId &&
     (from ts in _context.ValidationTemplateSet where ts.TemplateID == templateId     select ts.TemplateSetId).Contains((int)lf.TemplateSetID)


                                 select new TemplateChoiceDTO
                                 {
                                     ChoiceId = tc.ChoiceId,
                                     ChoiceText = tc.ChoiceText,
                                     NextQuestionId = lf.Goto

                                 };

相当于:

 select vc.RowID,Choice_Text,VCE.[Goto] from ropes.Validation_Choices vc
    left outer join ropes.Validation_Condition_Expression VCE on 
    VCE.ChoiceID = vc.RowID 
    where vc.QuestionID = 81 
    and TemplateSetID in (select RowID from ropes.Validation_Templates_Set where TemplateID = 66)

此查询返回1行,因为templatesetid的过滤器已移到

之外

0 个答案:

没有答案