简体   繁体   English

LINQ通过联接返回最近日期的记录

[英]LINQ returning records with most recent dates with a join

I am trying create a LINQ statement that returns a list of records (tasks) with their most recent assigned task history date. 我正在尝试创建LINQ语句,该语句返回具有最近分配的任务历史记录日期的记录(任务)列表。 So there are two tables the first is the task table which contains all of the information about a single task and then there is the task history table which keeps track of how and to who the task gets assigned to. 因此,有两个表,第一个是任务表,其中包含有关单个任务的所有信息,然后是任务历史表,用于跟踪任务分配的方式和对象。 Both of these tables have TaskID which is where they can be linked on a join. 这两个表都具有TaskID,可以在联接时链接它们。 The issue is that in the task history there can be many records with the same TaskID but I only want to return the record with most recent assigned date which is a column in the task history table. 问题是在任务历史记录中可以有许多具有相同TaskID的记录,但我只想返回具有最近分配日期的记录,该记录是任务历史记录表中的一列。 How can I do this? 我怎样才能做到这一点?

Here is what I have so far: 这是我到目前为止的内容:

var query = from task in db.AP_Tasks
            join taskH in db.AP_TaskHistory on task.TaskID equals taskH.TaskID
            orderby new {taskH.AssignDate}
            select new {task = task, assignedDate = taskH.AssignDate};

Update: 更新:

Most Recent Code: 最新代码:

        var query = from task in db.AP_Tasks
            from th in db.AP_TaskHistory
            where th.TaskID == task.TaskID
            orderby th.AssignDate
            select new {task = task, assignDate = th.AssignDate};

What you need here is a CROSS APPLY style query. 您需要的是CROSS APPLY样式查询。

select AP_Tasks.*, history.AssignDate
from AP_Tasks
cross apply (
   select top 1 AP_TaskHistory.AssignDate
   from AP_TaskHistory
   where AP_Tasks.TaskID = AP_TaskHistory.TaskID
   order by AP_TaskHistory.AssignDate DESC
) history

See here: How do I write this cross apply query in LINQ-to-SQL? 请参阅此处: 如何在LINQ-to-SQL中编写此交叉应用查询?

I think something like this would work for you. 我认为类似这样的方法对您有用。

var query = (from task in db.AP_Tasks
            join taskH in db.AP_TaskHistory on task.TaskID equals taskH.TaskID
            orderby new {taskH.AssignDate} descending
            select new {task = task, assignedDate = taskH.AssignDate}).First();

Be warned, I haven't tested this. 警告,我还没有测试过。

Instead of .First() you could try .FirstOrDefault() in order to more easily handle exceptions. 除了.First()您还可以尝试.FirstOrDefault()以便更轻松地处理异常。

Here is an example to get you going: 这是使您前进的示例:

List<Task> tasks = new List<Task>();
tasks.Add(new Task {TaskID = 1});
tasks.Add(new Task {TaskID = 3});
tasks.Add(new Task {TaskID = 5});

List<TaskHistory> taskHistories = new List<TaskHistory>();
taskHistories.Add(new TaskHistory { TaskID = 3, AssignDate = DateTime.Now });
taskHistories.Add(new TaskHistory { TaskID = 3, AssignDate = DateTime.Now.AddDays(3) });
taskHistories.Add(new TaskHistory { TaskID = 1, AssignDate = DateTime.Now });
taskHistories.Add(new TaskHistory { TaskID = 1, AssignDate = DateTime.Now.AddDays(1) });
taskHistories.Add(new TaskHistory { TaskID = 1, AssignDate = DateTime.Now.AddDays(4) });

var query = from task in tasks
            join thist in taskHistories on task.TaskID equals thist.TaskID
            group new { t = task, date = thist.AssignDate } by task.TaskID into grouped
            from g in grouped
            where g.date == grouped.Max(taskInGroup => taskInGroup.date)
            select g;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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