【问题标题】:i have a problem when filtering a list according to two dates in c#根据 c# 中的两个日期过滤列表时出现问题
【发布时间】:2022-01-15 19:55:59
【问题描述】:
var result = myList.Where(t =>  t.ReportDate >= StartDate)
                   .Where(t =>  t.ReportDate <= EndDate)

我正在尝试获取列表 (myList) 中的所有对象,这些对象的日期 (ReportDate) 在 StartDateEndDate 之间...我使用上面的代码效果很好但是当 @987654326 @ 和 EndDate 等于它返回 null。谁能帮忙...提前谢谢。

【问题讨论】:

  • 您的结束日期值是否包含时间部分? mm/dd/yyyy 23:59:59 还是只是 mm/dd/yyyy 00:00:00
  • 您是按日期还是日期时间过滤?
  • @HamletHakobyan 我正在使用DateTime?
  • 问题是关于你的意图而不是你使用的数据类型。
  • 一个小建议,在一个Where 中使用&amp;&amp; 而不是两个Where

标签: c# .net linq datetime


【解决方案1】:

我已尽我所能来整合您的 cmets。这将忽略日期的时间部分,并适用于 null ReportDates。

    DateTime now = DateTime.Now;
    // Get the Date part for the variables before the linq query
    DateTime startDate = now.Date;
    DateTime endDate = now.Date;
    
    List<Report> myList = new List<Report> { new Report(1, now), new Report(2, null), new Report(3, now.AddDays(1)) };

    // You are not required to make multiple Where calls
    // Check that ReportDate has a value before comparison
    // Get the Date part for the comparisons
    var reports = myList.Where(t => t.ReportDate.HasValue
                               && t.ReportDate.Value.Date >= startDate
                               && t.ReportDate.Value.Date <= endDate);
    
    foreach (var report in reports)
    {
        Console.WriteLine($"Report {report.ReportId}");
    }

报告 1

【讨论】:

    【解决方案2】:

    以下示例代码可能会让您走上正轨

    using System;
    using System.Collections.Generic;
    
    namespace Test
    {
        class Program
        {
            static void Main()
            {
                List<DateTime> dateTimeList = new List<DateTime>
                {
                    new DateTime(1946, 04, 11),
                    new DateTime(1948, 04, 22),
                    new DateTime(1975, 01, 23),
                    new DateTime(1975, 01, 23),
                    new DateTime(1977, 07, 07),
                    new DateTime(1988, 05, 02),
                };
                DateTime beginDate = new DateTime(1970, 01, 01);
                DateTime endDate = new DateTime(1979, 12, 31);
                List<DateTime> result = dateTimeList.FindAll(x => x >= beginDate && x <= endDate);
                foreach (DateTime dateTime in result)
                    Console.WriteLine(dateTime);
            }
    
        }
    }
    
    

    【讨论】:

      【解决方案3】:

      看来,你想排除 时间组件:

      var result = myList
        .Where(t => t.ReportDate.Date >= StartDate.Date &&
                    t.ReportDate.Date <= EndDate.Date);
      

      如果是你的情况,那么当StartDate.Date == EndDate.Date 你会得到resultReportDate.Date == StartDate.Date

      请注意,通常排除EndDate更方便:

      var result = myList
        .Where(t => t.ReportDate.Date >= StartDate.Date &&
                    t.ReportDate.Date < EndDate.Date);
      

      例如,如果我们想要 2021 年,我们可以输入 1.1.2021 .. 1.1.2022 而不是 1.1.2021 .. 31.12.2021

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-25
        • 1970-01-01
        • 2020-12-13
        • 2021-05-24
        • 2022-11-08
        • 1970-01-01
        • 2020-05-06
        相关资源
        最近更新 更多