2017-02-10 202 views
0

如何在linq查詢中構造下面的sql查詢以獲取結果?在linq查詢中使用子查詢

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

回答

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); 

希望這會有所幫助。

+0

但是SurveyID = 1在哪裏使用? SurveyID屬於LMS_SurveyQuestion。 – David

1

試試這個

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; 
+0

但是SurveyID = 1在哪裏使用? SurveyID屬於LMS_SurveyQuestion。 – David