【问题标题】:How do I turn this into LINQ?我如何把它变成 LINQ?
【发布时间】:2017-07-01 11:03:57
【问题描述】:

如何将我的 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

【问题讨论】:

  • 我什至不知道这样的 SQL 是可能的:)
  • 您能否添加一个数据集示例以及您希望返回哪些数据?只是为了让我们有更全面的了解。
  • 就数据而言,@nbokmans 这样做会返回一个 JSON obj: return Json(new { data = data, tStudents = tStudents }, JsonRequestBehavior.AllowGet);然后在客户端上,它是一个向用户显示的数据表。但据我的理解,这无关紧要,因为一旦执行 LINQ 并且客户端不在此阶段,我应该在 tStudents obj 中查看我的行。
  • 您在 ON 原因中的条件可能完全在 WHERE 子句中。
  • 不,我的意思是现在我不确定您想要的结果是什么。你能把你的查询写成一个句子吗?

标签: c# sql entity-framework linq linq-to-sql


【解决方案1】:

我的评论的扩展:

我认为在语义上将条件放在 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();

【讨论】:

  • 这带回了太多行。请在原始帖子中查看我的编辑。
  • @Shucoder 也许你只需要添加 Distinct() 即可。我编辑了帖子。
  • 很遗憾没有。结果缩短了,但这不是正确的结果,而且我已经知道不同的结果和方法。你看到我的示例表了吗?我认为这里令人困惑的是学生表少了一列,但我确信它以前已经完成了。
  • @Shucoder 您的 SQL 查询是否提供了正确的结果集? (这个 linq 应该提供相同的)
  • 我收回我之前的评论@jannagy02!你的回答是正确的!我刚刚错过了我的 cookie,午餐后我发现了它,现在一切都按预期工作,并且在 LINQ 中返回了正确的行 ....快乐的日子。
【解决方案2】:

这甚至与您想要的东西很接近吗?我已尽力根据您在 OP 的 cmets 中的描述。

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

【讨论】:

  • 这是与我的查询最接近的结果,但仍然很少,也有一些错误的结果。请查看我在原始帖子中的编辑,它应该通过显示结果表来帮助每个人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 2014-01-28
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
相关资源
最近更新 更多