C# C语言中的泛型日期时间列表比较#

C# C语言中的泛型日期时间列表比较#,c#,C#,我有一个通用的日期时间列表 List<DateTime> **PreviousDates** = //Some List of Dates List**PreviousDates**=//一些日期列表 现在,我想获取先前日期列表中的所有日期,其月份和年份等于给定日期的月份和年份。无论什么日子和时间 我尝试使用List.Contains()方法,但它检查日期的所有部分,我只想检查月份和年份 救救我 谢谢 Pradeep您需要使用该方法。它将过滤列表,您可以将年份和月份与当前日期进行

我有一个通用的日期时间列表

List<DateTime> **PreviousDates** = //Some List of Dates
List**PreviousDates**=//一些日期列表
现在,我想获取先前日期列表中的所有日期,其月份和年份等于给定日期的月份和年份。无论什么日子和时间

我尝试使用List.Contains()方法,但它检查日期的所有部分,我只想检查月份和年份

救救我

谢谢 Pradeep

您需要使用该方法。它将过滤列表,您可以将年份和月份与当前日期进行比较

DateTime currentDate = DateTime.Now;
var filteredDates = previousDates.Where(
    d => d.Year == currentDate.Year && d.Month == currentDate.Month);
你需要使用这个方法。它将过滤列表,您可以将年份和月份与当前日期进行比较

DateTime currentDate = DateTime.Now;
var filteredDates = previousDates.Where(
    d => d.Year == currentDate.Year && d.Month == currentDate.Month);

可以使用LINQ返回IEnumerable

PreviousDates.Where(d => d.Month == month && d.Year == year);

可以使用LINQ返回IEnumerable

PreviousDates.Where(d => d.Month == month && d.Year == year);

您可以为间隔的开始和结束创建
DateTime
值:

DateTime start = new DateTime(GivenDate.Year, GivenDate.Month, 1);
DateTime end = start.AddMonths(1);
List<DateTime> inside =
  PreviousDates
  .Where(d => d >= start && d < end)
  .ToList();
现在,您可以轻松选择间隔内的项目:

DateTime start = new DateTime(GivenDate.Year, GivenDate.Month, 1);
DateTime end = start.AddMonths(1);
List<DateTime> inside =
  PreviousDates
  .Where(d => d >= start && d < end)
  .ToList();
内部列表=
以前的日期
。其中(d=>d>=开始和&d

(当然,您可以将
GivenDate.Year
GivenDate.Month
值与
Where
中的
d.Year
d.Month
进行比较,但这是一个更复杂、效率更低的条件。)

您可以为间隔的开始和结束创建
DateTime
值:

DateTime start = new DateTime(GivenDate.Year, GivenDate.Month, 1);
DateTime end = start.AddMonths(1);
List<DateTime> inside =
  PreviousDates
  .Where(d => d >= start && d < end)
  .ToList();
现在,您可以轻松选择间隔内的项目:

DateTime start = new DateTime(GivenDate.Year, GivenDate.Month, 1);
DateTime end = start.AddMonths(1);
List<DateTime> inside =
  PreviousDates
  .Where(d => d >= start && d < end)
  .ToList();
内部列表=
以前的日期
。其中(d=>d>=开始和&d

(当然,您可以将
GivenDate.Year
GivenDate.Month
值与
Where
中的
d.Year
d.Month
进行比较,但这是一个更复杂、效率更低的条件。)

您可以尝试类似的方法

DateTime criteria = DateTime.Today;
List<DateTime> filtered = previousDates.Where(dt => dt.Month == criteria.Month && dt.Year == criteria.Year).ToList();
DateTime条件=DateTime.Today;
过滤列表=以前的日期。其中(dt=>dt.Month==criteria.Month&&dt.Year==criteria.Year)。ToList();

您可以尝试以下方法

DateTime criteria = DateTime.Today;
List<DateTime> filtered = previousDates.Where(dt => dt.Month == criteria.Month && dt.Year == criteria.Year).ToList();
DateTime条件=DateTime.Today;
过滤列表=以前的日期。其中(dt=>dt.Month==criteria.Month&&dt.Year==criteria.Year)。ToList();

缺少端部支架和分号缺少端部支架和分号不需要。还有很多其他的方法,不需要。还有很多其他的方法。我认为这很难理解,因为它没有明确说明它的目的。还有,一个小错误,应该是个月。@Joey:谢谢,更正了错误。我知道你怎么能认为这比需要的稍微复杂一点,但它更灵活。如果您想得到跨越两个或三个月的日期,那么使用此代码非常简单,但是如果您比较年和月的值,它会变得更加复杂。我认为这更难理解,因为它没有明确说明其目的。还有,一个小错误,应该是个月。@Joey:谢谢,更正了错误。我知道你怎么能认为这比需要的稍微复杂一点,但它更灵活。如果您想获取跨越两个或三个月的日期,使用此代码非常简单,但如果比较年和月的值,则会变得更加复杂。不要直接在查询中使用
DateTime.Today
。它可以在查询的中途改变,给你一个非常奇怪的结果。将其存储在变量中,并在查询中使用该变量。不要直接在查询中使用
DateTime.Today
。它可以在查询的中途改变,给你一个非常奇怪的结果。将其存储在变量中,并在查询中使用该变量。