如何将我的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发布于 2017-02-13 13:15:55
以下是我的评论:
我认为把条件放在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();发布于 2017-02-13 13:13:25
这是你想要的东西吗?我尽我最大的努力从你的描述在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();https://stackoverflow.com/questions/42204437
复制相似问题