[英]How do I turn this into LINQ?
如何將我的SQL轉換為LINQ?
DECLARE @cookie nvarchar(50)
SET @cookie = 'test@test.com'
SELECT s.firstname
FROM [examManager].[dbo].[students] AS s
JOIN [examManager].[dbo].tutors t ON s.last_exam IN (t.default_exam_id ,
t.last_exam, t.next_exam)
OR s.next_exam IN (t.default_exam_id , t.last_exam , t.next_exam)
--WHERE t.email = @cookie
我沿着這條路線走(在查詢下),但是與SQL結果進行比較時,它並沒有帶回我需要的東西。 我將在C#中處理cookie,這不是問題。
var tStudents = from s in student
join t in tutor on s.last_exam equals t.default_exam_id //{ ColA = s.last_exam, ColB = s.next_exam } equals new { ColA = t.last_exam, ColB = t.next_exam }
join t2 in tutor on s.last_exam equals t2.last_exam
join t3 in tutor on s.last_exam equals t3.next_exam
//where t.email == finalCookie
select new
{
s.firstname,
s.lastname,
};
+++編輯+++為了使以上工作正常,請考慮以下兩個示例表。
導師
------------------------------------------------------------
id |email | |default_exam_id| |last_exam|next_exam
------------------------------------------------------------
0 |test@test.com |903 |910 |903
------------------------------------------------------------
學生們
------------------------------------------------------------
id |fname | |last_exam |next_exam
------------------------------------------------------------
0 |john |903 |910
1 |doe |912 |903
2 |gary com |909 |988
------------------------------------------------------------
結果應該如下:
0 |john |903 |910
1 |doe |912 |903
這甚至是您想要的東西附近嗎? 根據您在OP的評論中的描述,我已盡力而為。
List<string> students =
studentList.Where(
s =>
tutorList.Any(
t =>
t.last_exam == s.last_exam || t.next_exam == s.last_exam ||
t.default_exam_id == s.last_exam || t.last_exam == s.next_exam ||
t.next_exam == s.next_exam || t.default_exam_id == s.next_exam))
.Select(n => n.firstname + " " + n.lastname)
.ToList();
擴展我的評論:
我認為從語義上將條件放在where子句中也更正確。
var tStudents = from s in student
from t in tutor
where (s.last_exam == t.default_exam_id || s.last_exam == t.last_exam || s.last_exam == t.next_exam
|| s.next_exam == t.default_exam_id || s.next_exam == t.last_exam || s.next_exam == t.next_exam)
//&& t.email == finalCookie
select new
{
s.firstname,
s.lastname,
};
更新:
var result = tStudents.Distinct();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.