等效 LINQ 查询

发布于 2024-11-06 15:09:06 字数 234 浏览 0 评论 0原文

我在 SQL Server 2008 中有一个针对学生的 history

StudentHistoryId, StudentId, Grade, CreatedAt, ModifiedAt, ModifiedBy, Active

。我是 LINQ 新手。

如何编写 LINQ 查询来获取所有活跃学生的最新修改行以及相同的等效 sql 查询?

I have a history table for Students in SQL Server 2008.

StudentHistoryId, StudentId, Grade, CreatedAt, ModifiedAt, ModifiedBy, Active

I am new to LINQ.

How do I write a LINQ query to get the latest modified row for all the active students and also the equivalent sql query for the same ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红尘作伴 2024-11-13 15:09:06

类似于(假设 LINQ-SQL):

using (YourDataContext db = new YourDataContext())
{
   var data = from s in db.Students
              select new 
              {
                  StudentId = s.StudentId,
                  LastHistory = s.Histories
                                 .OrderByDescending(s => s.ModifiedAt)
                                 .Where(s => s.Active)
                                 .FirstOrDefault()
              };
}

这是假设您想要所有学生,无论他们实际上是否有任何历史记录。如果不想这样做,您可以从历史记录表开始,然后按学生 ID 进行分组。

要查看 SQL,您可以将鼠标悬停在调试中的变量以查看生成的 SQL。我懒得转换 LINQ ;-)

Something like (Assuming LINQ-SQL):

using (YourDataContext db = new YourDataContext())
{
   var data = from s in db.Students
              select new 
              {
                  StudentId = s.StudentId,
                  LastHistory = s.Histories
                                 .OrderByDescending(s => s.ModifiedAt)
                                 .Where(s => s.Active)
                                 .FirstOrDefault()
              };
}

This is assuming that you want all students, regardless of whether they actually have any history. If don't want this, you can start with the History table and group by Student ID.

To view the SQL, you can hover the variable in debugging to see the SQL produced. I'm too lazy to convert the LINQ ;-)

深居我梦 2024-11-13 15:09:06
var q = 
    from h in history 
    where h.Active
    group h by new { h.StudentId, h.Grade } into g
    select new 
    {
        StudentId = g.Key.StudentId,
        Grade = g.Key.Grade,
        LatestModified = g.Max (x => x.ModifiedAt)
    }
var q = 
    from h in history 
    where h.Active
    group h by new { h.StudentId, h.Grade } into g
    select new 
    {
        StudentId = g.Key.StudentId,
        Grade = g.Key.Grade,
        LatestModified = g.Max (x => x.ModifiedAt)
    }
↙温凉少女 2024-11-13 15:09:06

LINQ

 var tempresult = (from student in Students
                   where Active == true).OrderByDesc(ModifiedAt)
 List<Student> results = new List<Student>();
 foreach(var result in tempResult)
 {
    if((results.Where(r => r.StudentId == result.StudentId).FirstOrDefault()) == null)
    {
       results.Add(result);
    }
 }

SQL

 Select [Rows]
 From Students S
 Where S.Active = 1
 And S.ModifiedAt = (Select Max(ModifiedAt) 
                     From Student S1
                     Where S1.StudentId = S.StudentId)

Linq 很老套(我确信有更好的方法,但我不记得了),而且我只是对 SQL 语法有一定的信心(尽管这应该为您指明正确的方向,即使它并不完全正确),但其中任何一个都应该得到:当前活跃的每个学生的最大 ModifiedAt 。

.FirstOrDefault() [LINQ] 或 Top 1 只会选择具有最新 ModifiedAt 的单行(仅一个学生)。

LINQ

 var tempresult = (from student in Students
                   where Active == true).OrderByDesc(ModifiedAt)
 List<Student> results = new List<Student>();
 foreach(var result in tempResult)
 {
    if((results.Where(r => r.StudentId == result.StudentId).FirstOrDefault()) == null)
    {
       results.Add(result);
    }
 }

SQL

 Select [Rows]
 From Students S
 Where S.Active = 1
 And S.ModifiedAt = (Select Max(ModifiedAt) 
                     From Student S1
                     Where S1.StudentId = S.StudentId)

The Linq is hacky (and I'm sure there's a better way, but I can't remember it) and I'm only sort-of confident about the SQL syntax (though that should point you in the right direction even if it's not exactly right), but either of those should get: The maximum ModifiedAt for every student that is currently active.

.FirstOrDefault() [LINQ] or Top 1 would only select the single row (only one student) with the most recent ModifiedAt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文