简体   繁体   中英

How do I turn this into LINQ?

How will convert my SQL into 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

I am going down this route (below query) but it's not bringing back what I need when compare to the SQL results. I'll handle the cookie in the C# that's not an issue.

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,
                };

+++EDIT+++ For the above to work please consider these two sample tables.

Tutors

------------------------------------------------------------
id    |email |        |default_exam_id| |last_exam|next_exam
------------------------------------------------------------
0     |test@test.com  |903              |910      |903
------------------------------------------------------------

Students

------------------------------------------------------------
id    |fname |        |last_exam        |next_exam
------------------------------------------------------------
0     |john           |903              |910      
1     |doe            |912              |903      
2     |gary com       |909              |988      
------------------------------------------------------------

Result/s should be as follows:

0     |john           |903              |910      
1     |doe            |912              |903

Is this even anything remotely close to what you want? I tried my best going from your description in the comments of the 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();

Extension to my comment:

I think that putting the condition in the where clause semantically is more correct also.

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,
                };

Update:

var result = tStudents.Distinct();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM