使用LINQ从一系列日期中获取不同的日期

时间:2017-11-22 09:31:33

标签: c# asp.net-mvc linq biometrics

我一直在尝试根据表格中的最新时间获取不同日期的列表,其中我在给定范围内重复日期。即。

鉴于范围;

Start Date: 11/1/2017 12:00:00 AM
End Date: 11/20/2017 12:00:00 AM 

使用查询;

var mod = (from m in lstMachineInfo
             where m.RegisterationId == EnrollNumber 
                && m.Date >= startDate 
                && m.Date <= endDate
             select m).Distinct();

我得到的是什么;

11/9/2017 8:02:09 AM    
11/9/2017 3:22:51 PM    
11/9/2017 5:09:23 PM    
11/10/2017 11:23:04 AM  
11/10/2017 4:19:57 PM   
11/14/2017 10:11:11 AM  
11/14/2017 6:30:30 PM   

我想要的是什么;

11/9/2017 8:02:09 AM    
11/10/2017 11:23:04 AM  
11/14/2017 10:11:11 AM

2 个答案:

答案 0 :(得分:2)

var earlyMod = mod
    .GroupBy(dt => dt.Value.Date)
    .Select(z => z.OrderBy(y => y.Date).First())
    .ToList();

earlyMod将返回每个指定日期的最早时间。这将在数​​据库之外执行(如果给定日期中不同时间记录的数量有限,则会很好。)

如果您希望它执行数据库中的 ,请尝试:

var mod = (from m in lstMachineInfo
             where m.RegisterationId == EnrollNumber 
                && m.Date >= startDate 
                && m.Date <= endDate
             select m)
           .GroupBy(a => EntityFunctions.TruncateTime(a.Value.Date))
           .Select(z => z.OrderBy(y => y.Date).First()).ToList();

答案 1 :(得分:1)

您可以使用MoreLinq的DistinctBy。 (您可以通过NuGet安装它。)

然后你可以这样做:

using System;
using MoreLinq;

namespace Demo
{
    class Program
    {
        static void Main()
        {
            var dates = new []
            {
                new DateTime(2017, 11, 9, 8, 2, 9),
                new DateTime(2017, 11, 9, 5, 9, 23),
                new DateTime(2017, 11, 9, 15, 22, 51),
                new DateTime(2017, 11, 9, 17, 9, 23),
                new DateTime(2017, 11, 10, 11, 23, 04),
                new DateTime(2017, 11, 10, 16, 19, 57),
                new DateTime(2017, 11, 14, 10, 11, 11),
                new DateTime(2017, 11, 14, 18, 30, 30)
            };

            var distinctByDate = dates.OrderBy(date => date).DistinctBy(date => date.Date);

            foreach (var date in distinctByDate)
                Console.WriteLine(date);
        }
    }
}

或者如果日期可以为空,则必须过滤掉空的日期:

        var dates = new DateTime?[]
        {
            new DateTime(2017, 11, 9, 17, 9, 23),
            new DateTime(2017, 11, 9, 15, 22, 51),
            new DateTime(2017, 11, 9, 8, 2, 9),
            null,
            new DateTime(2017, 11, 10, 11, 23, 04),
            new DateTime(2017, 11, 9, 17, 9, 23),
            null,
            new DateTime(2017, 11, 14, 10, 11, 11),
            new DateTime(2017, 11, 10, 16, 19, 57),
            new DateTime(2017, 11, 14, 18, 30, 30)
        };

        var distinctByDate = dates.Where(date => date.HasValue).OrderBy(date => date).DistinctBy(date => date?.Date);

        foreach (var date in distinctByDate)
            Console.WriteLine(date);
    }

请注意,如果保证日期已经有序,您可以省略OrderBy()

为了完整起见,这里是DistinctBy的实现(我相信这首先由Jon Skeet提供):

public static class EnumerableDistinctExt
{
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        return source.DistinctBy(keySelector, null);
    }

    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
        this IEnumerable<TSource> source, 
        Func<TSource, TKey> keySelector, 
        IEqualityComparer<TKey> comparer)
    {
        return distinctByImpl(source, keySelector, comparer);
    }

    static IEnumerable<TSource> distinctByImpl<TSource, TKey>(
        IEnumerable<TSource> source,
        Func<TSource, TKey> keySelector,
        IEqualityComparer<TKey> comparer)
    {
        var knownKeys = new HashSet<TKey>(comparer);
        return source.Where(element => knownKeys.Add(keySelector(element)));
    }
}