【问题标题】:Use subquery in linq query在 linq 查询中使用子查询
【发布时间】:2017-02-10 06:33:27
【问题描述】:

如何在 linq 查询中构造以下 sql 查询以获得结果?

SELECT PageNumber from LMS_SurveyQuestion WHERE SurveyQuestionID IN
(SELECT  SurveyQuestionID from LMS_SurveyQuestionOptionChoice 
WHERE NextPageNumber = 4) and SurveyID = 1

【问题讨论】:

    标签: c# .net linq linq-to-entities


    【解决方案1】:

    看看this article。基本上,如果要在 LINQ 中实现 SQL IN 查询,则需要先构造一个内部查询,然后使用 Contains() 方法。这是我的尝试:

    var innerQuery = (from log in LMS_SurveyQuestionOptionChoice where log.NextPageNumber = 4 select log.SurveyQuestionID);
    
    var result = (from f in LMS_SurveyQuestion where innerQuery.Contains(f.SurveyQuestionID) && f.SurveyID = 1 select f);
    

    希望这会有所帮助。

    【讨论】:

    • 但是SurveyID = 1 在哪里使用? SurveyID 属于 LMS_SurveyQuestion。
    【解决方案2】:

    试试这个

    var result = from l in LMS_SurveyQuestion 
                 let lsq = from l_S in LMS_SurveyQuestionOptionChoice 
                 where l_S.NextPageNumber = 4
                 select l_S.SurveyQuestionID 
                 where lsq.Contains(l.SurveyQuestionID) and l.surveyid = 1
                 select l.PageNumber;  
    

    【讨论】:

    • 但是SurveyID = 1 在哪里使用? SurveyID 属于 LMS_SurveyQuestion。
    猜你喜欢
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多