【问题标题】:How to group items in a list with the same max date and filter them如何对具有相同最大日期的列表中的项目进行分组并过滤它们
【发布时间】:2015-04-29 14:55:00
【问题描述】:

我有 List<TakenBMI> 有这 4 列和数据:

TakenDate     UerID   TakenItem   TakenValue
Aug-10-2014     34     Weight       140
Aug-10-2014     34     Height       5.5
Mar-15-2015     34     Weight       141
Mar-15-2015     34     Height       5.5

我想根据 TakenDate 将它们分组到单独的列表中,并找出我应该使用哪个列表,其中包含最新拍摄日期的详细信息。

这是我尝试过的:

var q = from n in TakenBMI 
        group n by n.TakenDate into g 
        select g.OrderByDescending(t=>t.TakenDate )
                .FirstOrDefault(); 

var m = from n in TakenBMI 
        group n by n.TakenDate into g 
        select new { TakenDate = Convert.ToDateTime(q) };

另外,如果有人可以在获得最大日期的列表后提出建议,如果可能的话,我如何获得第二个最新日期的列表?感谢所有回复的人

【问题讨论】:

  • 到目前为止您尝试过什么?建议你看看GroupBy
  • 另外,TakenDate 是 DateTime 吗?
  • 是的,TakenDate 是一个日期时间。我试过这个:
  • var q = from n in TakenBMI group n by n.TakenDate into g select g.OrderByDescending(t=>t.TakenDate ).FirstOrDefault(); var m = from n in TakenBMI group n by n.TakenDate into g select new { TakenDate = Convert.ToDateTime(q) };
  • 你能在 m 中添加你收到的内容吗,这样我们就不必自己运行了???

标签: c# .net linq list ienumerable


【解决方案1】:

我会通过查找最新日期来接近:

var latestDate = items.Select(i => i.TakenDate).Max();

然后通过过滤具有该日期的项目:

var itemsWithLatestDate = items.Where(i => i.TakenDate == latestDate).ToList();

如果您要求所有这些都按日期顺序分组,那么:

var itemsByDate = items.GroupBy(i => i.TakenDate).OrderBy(g => g.Key);

最新日期组将是这些组中的最后一个:

var itemsWithLatestDate = itemsByDate.Last();

【讨论】:

  • 我在想它应该类似于首先找到最大日期,将尝试这个。谢谢
【解决方案2】:

我通常使用字典

   class Program
    {
        static void Main(string[] args)
        {
            List<TakenBMI> data = new List<TakenBMI>() {
               new TakenBMI() {TakenDate = DateTime.Parse("Aug-10-2014"), UerID = 34,  TakenItem = "Weight", TakenValue = 140},
               new TakenBMI() {TakenDate = DateTime.Parse("Aug-10-2014"), UerID = 34,  TakenItem = "Height", TakenValue = 5.5},
               new TakenBMI() {TakenDate = DateTime.Parse("Aug-15-2014"), UerID = 34,  TakenItem = "Weight", TakenValue = 141},
               new TakenBMI() {TakenDate = DateTime.Parse("Aug-15-2014"), UerID = 34,  TakenItem = "Height", TakenValue = 5.5},
            };

            Dictionary<DateTime, List<TakenBMI>> dict = data.AsEnumerable()
                .GroupBy(x => x.TakenDate, y => y)
                .ToDictionary(x => x.Key, y => y.ToList());

        }
    }
    public class TakenBMI
    {
        public DateTime TakenDate {get;set;}
        public int UerID {get;set;}
        public string TakenItem {get;set;}
        public double TakenValue {get;set;} 
    }
}

【讨论】:

    【解决方案3】:

    我想根据 TakenDate 将它们分组到单独的列表中

    您可以分组 TakenDate 使用GroupBy() LINQ 查询运算符并按如下方式枚举结果:

    var groups = TakenBMIList.GroupBy(x => x.TakenDate);
    
    foreach (var group in groups)
    {
        Console.WriteLine("TakenDate: {0}", group.Key);
    
        List<TakenBMI> list = group.ToList(); //List of TakenBMI with current TakenDate
    }
    

    找出我应该使用哪个列表,其中包含最新的详细信息 拍摄日期

    您可以找到 latest (max) TakenDate 的元素s,如下所示:

    var latestTakenBMIs = TakenBMIList.Where(x => x.TakenDate == TakenBMIList.Max(y => y.TakenDate)).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      相关资源
      最近更新 更多