Linq查询:选择最大(日期)时ID的内容

时间:2018-01-02 15:58:39

标签: c# sql linq

大家新年快乐! : - )

我搜索了几个小时,但我无法弄清楚如何使用Linq正确构建我的查询。

我有两张桌子:A和B

在表A中:我有A_ID,B_ID,VALUE_DATE。

在表B中我有B_ID,状态。

示例:

A_ID = 547 VALUE_DATE = 01/05/10 B_ID = 14750

A_ID = 547 VALUE_DATE = 01/10/10 B_ID = 14750

我想使用B_ID加入A和B,其中VALUE_DATE是最大值。 目前,这就是我所拥有的:

context是OracleDataContextCommon对象 evalId和listAffId是我的webservice方法的参数。

var query = (from tabA in (from a in context.A
                          where listAffId.Contains(a.A_ID)
                          group a by a.A_IDinto grp
                          select new
                          {
                              pId = (int)grp.Key,
                              valDate = grp.Max(x => x.ValueDate),
                              bId = grp.Select(x => B_ID).FirstOrDefault()
                          })
                         join tabB in context.B on tabA.bIdequals equals tabB.B_ID
                         select new
                         {
                             affId = tabA.pId,
                             status = tabB.Status
                         });

我哪里错了?感谢您的调查......

3 个答案:

答案 0 :(得分:0)

问题在于您是否采用了群组的最长日期,但是您在群组中选择了FirstOrDefault B_ID ,这并非必然相关与具有最大日期的A行。最简单的方法可能是在分组之前按日期降序排序数据,然后将每个组中的第一项用于日期和b_id

请注意,我已使用下面的虚拟数据切换了您的上下文。

var theAs = new[]
{
   new {A_ID = 547, ValueDate = new DateTime(2010, 05, 01), B_ID = 14750}, 
   new {A_ID = 547, ValueDate = new DateTime(2010, 10, 01), B_ID = 14751}
};
var theBs = new[]
{
  new {B_ID = 14750, Status = "Foo"}, 
  new {B_ID = 14751, Status = "Bar"}
};
var listAffId = new[]{547};

var query = (from tabA in (from a in theAs
             where listAffId.Contains(a.A_ID)
             // Order the data - the first row per group is thus max date
             orderby a.ValueDate descending
             group a by a.A_ID into grp
             select new
             {
                 pId = (int)grp.Key,
                 // Now pull the date and B_ID from the same row
                 valDate = grp.First().ValueDate,
                 bId = grp.First().B_ID
             })
             join tabB in theBs on tabA.bId equals tabB.B_ID
             select new
             {
                 affId = tabA.pId,
                 status = tabB.Status
             });

答案 1 :(得分:0)

我用类模拟代码。我认为这应该有用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Context context = new Context();
            List<int> listAffId = new List<int>() { 547 };

            var query = (from a in context.A where listAffId.Contains(a.A_ID)
                         join b in context.B on a.B_ID equals b.B_ID
                         select new { a = a, b = b})
                         .GroupBy(x => x.a.A_ID)
                         .Select(x => new { x = x, max = x.Max(y => y.a.VALUE_DATE)})
                         .Select(x => new {
                             affId = x.x.Key,
                             status = x.x.Where(y => y.a.VALUE_DATE == x.max).Select(y => y.b.STATUS).FirstOrDefault()
                         }).ToList();



        }
    }
    public class Context
    {
        public List<TableA> A { get; set; }
        public List<TableB> B { get; set; }
    }
    public class TableA
    {
        public int A_ID { get; set; }
        public int B_ID { get; set; }
        public DateTime VALUE_DATE { get; set; }
    }
    public class TableB
    {
        public int B_ID { get; set; }
        public string STATUS { get; set; }
    }
}

答案 2 :(得分:0)

您需要选择最长日期的B_ID,因此请先找到最长日期:

var query = (from tabA in (from a in context.A
                          where listAffId.Contains(a.A_ID)
                          group a by a.A_IDinto grp
                          let maxdate = grp.Max(a => a.ValueDate)
                          select new
                          {
                              pId = (int)grp.Key,
                              valDate = maxdate,
                              bId = grp.Where(a => a.ValueDate == maxdate).Select(x => B_ID).FirstOrDefault()
                          })
                         join tabB in context.B on tabA.bId equals equals tabB.B_ID
                         select new
                         {
                             affId = tabA.pId,
                             status = tabB.Status
                         });