简体   繁体   English

使用多个连接和多个选择字段从 linq 获取最大日期

[英]Get the max date from linq with mutiple joins and with multiple select fields

I have been scratching my head with this and cannot find a solution.我一直在为此挠头,找不到解决方案。 I have a dataset like this,我有一个这样的数据集,

JobID   DueDate     VisitID
4024082 2020-07-31  5267157
4024082 2020-07-29  5263915
4024082 2020-08-04  5269154
4024082 2020-07-23  5258774
4024082 2020-07-22  5255307

I want to get the result set like this,我想得到这样的结果集,

JobID   Duedate     VisitID
4024082 2020-08-04  5269154

I have tried several ways but cannot get it to work.我尝试了多种方法,但无法使其正常工作。 my linq query is我的 linq 查询是

            var jobs = await (from j in _ctx.Jobs
                              join v in _ctx.Visits on j.JobID equals v.JobID
                              join p in _ctx.Products on j.ProductID equals p.ProductID
                              where j.IsProcessed== true
                              group v by new
                              {
                                  v.JobID,
                                  v.VisitID
                              }
                into g
                              select new
                              {
                                  JobId = g.Key,
                                  DueDate = g.Max(x => x.DueDate),
                                  VisitID = g.Key.VisitID
                              }).ToListAsync();

Any help would be highly appreciated.任何帮助将不胜感激。 Thanks谢谢

You cannot use Max to get more than one value.您不能使用Max获得多个值。 You have to use a sorting mechanism here.您必须在这里使用排序机制。

The following is an example of how to get the jobs grouped by the JobID and then every highest date with accompanying VisitID .下面是一个示例,说明如何获取按JobID分组的JobID ,然后是每个最高日期以及随附的VisitID

Here is the example.这是示例。 (I used LinqPad to test this): (我用 LinqPad 来测试这个):

void Main()
{
    var jobs = new[]
    {
        new Job { JobID = 4024082, DueDate = DateTime.ParseExact("2020-07-31", "yyyy-MM-dd", CultureInfo.InvariantCulture), VisitID = 5267157 },
        new Job { JobID = 4024082, DueDate = DateTime.ParseExact("2020-07-29", "yyyy-MM-dd", CultureInfo.InvariantCulture), VisitID = 5263915 },
        new Job { JobID = 4024082, DueDate = DateTime.ParseExact("2020-08-04", "yyyy-MM-dd", CultureInfo.InvariantCulture), VisitID = 5269154 },
        new Job { JobID = 4024082, DueDate = DateTime.ParseExact("2020-07-23", "yyyy-MM-dd", CultureInfo.InvariantCulture), VisitID = 5258774 },
        new Job { JobID = 4024082, DueDate = DateTime.ParseExact("2020-07-22", "yyyy-MM-dd", CultureInfo.InvariantCulture), VisitID = 5255307 },
    };
    
    var query = from job in jobs
                group job by job.JobID into grp
                select grp.OrderByDescending(g => g.DueDate).FirstOrDefault();
    
    query.Dump();
}

public class Job
{
    public int JobID { get; set; }
    public DateTime DueDate { get; set; }
    public int VisitID { get; set; }
}

Output:输出:

JobID    DueDate            VisitID
4024082  4-8-2020 00:00:00  5269154 

I think the problem is that you're using VisitId as part of the GroupBy .我认为问题在于您使用VisitId作为GroupBy一部分。 Since all the VisitId values are unique, you'll get a single record per group (and therefore one group for each record).由于所有VisitId值都是唯一的,您将获得每个组的单个记录(因此每个记录对应一个组)。

Perhaps you should only join on JobId if you want one group from those sample records.如果您想要这些示例记录中的一组,也许您应该只加入JobId Then to get the VisitID for the row that had the most recent DueDate , we can order the group by DueDate (descending) and take the VisitID from the First() record:然后拿到VisitID对于有最新的行DueDate ,我们可以订购由该组DueDate (下降),并采取VisitIDFirst()的记录:

var jobs = await (from j in _ctx.Jobs
                  join v in _ctx.Visits on j.JobID equals v.JobID
                  join p in _ctx.Products on j.ProductID equals p.ProductID
                  where j.IsProcessed== true
                  group v by v.JobID
                  into g
                      select new
                      {
                          JobId = g.Key,
                          DueDate = g.Max(x => x.DueDate),
                          VisitID = g.OrderByDecending(x => x.DueDate).First().VisitID
                      }).ToListAsync();

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

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