根据给定的日期从列表中查找项目[英] Find items from the list based on given date

本文是小编为大家收集整理的关于根据给定的日期从列表中查找项目的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

为了清楚地显示,我有以下列表项.我可以想象下面的小列表,可能有数百行.

CourseId    ClassName    StartDate
--------    --------      --------  
12321       Math         08-25-2017 
14321       Math         08-25-2017   
32342       Physics      08-25-2017  
34345       Chemistry    08-25-2017 
25325       Math         01-25-2018
45329       Math         01-25-2018     
33325       Math         01-25-2018          
44345       Chemistry    01-25-2018    

我有 ClassName 和 Date 要传递以检索相应的对象.我很难在 LINQ 中实现 Date 参数.以下实现仅排序并返回一项.

public List<Course> GetClassesByNameAndDate(string className, DateTime date, List<Courses> allCourses)
{
    List<Course> courses  = allCourses.Where( x=> x.ClassName == className && x.StartDate <= date ).OrderByDescending(x=> x.StartDate ).ToList();

}

如果我将 today date 和课程名称作为 Math 传递,那么它应该从列表中返回 25325,45329,33325 courseIDs 对象.

在另一个例子中,如果我给出一个日期 01-01-2018,那么它应该返回 12321, 14321 对象.因为 01-01-2018 早于 25325,45329,33325 开始日期,即 01-25-2018.

推荐答案

我想我终于明白了这个要求.你不想要只有一个项目.您也不希望列表包含 startDate 为 <= date 的所有项目.但是您想要具有最接近开始日期的整个项目组(如果所有项目都有相同的startDate).你可以GroupBy日期:

List<Course> courses  = allCourses
    .Where( x=> x.ClassName == className && x.StartDate <= date )
    .OrderByDescending(x=> x.StartDate)
    .GroupBy(x => x.StartDate)
    .FirstOrDefault()?.ToList() ?? new List<Course>();

本文地址:https://www.itbaoku.cn/post/1557161.html

问题描述

I have the following List item in order to display clearly. I could visualize the small list follows, that could be hundreds rows.

CourseId    ClassName    StartDate
--------    --------      --------  
12321       Math         08-25-2017 
14321       Math         08-25-2017   
32342       Physics      08-25-2017  
34345       Chemistry    08-25-2017 
25325       Math         01-25-2018
45329       Math         01-25-2018     
33325       Math         01-25-2018          
44345       Chemistry    01-25-2018    

I have ClassName and Date to pass to retrieve the corresponding objects. I am having difficulty how to implement Date parameter into the LINQ. The following implementation sorts and returns only one item.

public List<Course> GetClassesByNameAndDate(string className, DateTime date, List<Courses> allCourses)
{
    List<Course> courses  = allCourses.Where( x=> x.ClassName == className && x.StartDate <= date ).OrderByDescending(x=> x.StartDate ).ToList();

}

If I pass today date and also course name as Math, then it should return me 25325,45329,33325 courseIDs objects from the list.

In other example, if I give a date 01-01-2018, then it should return 12321, 14321 objects. Because 01-01-2018 is earlier than the 25325,45329,33325 startdate which is 01-25-2018.

推荐答案

I guess finally i have understood the requirement. You don't want only one item. You also don't want the list with all items where the startDate is <= date. But you want the whole group of items with the closest start-date(if all of them have the same startDate). You can GroupBy the date:

List<Course> courses  = allCourses
    .Where( x=> x.ClassName == className && x.StartDate <= date )
    .OrderByDescending(x=> x.StartDate)
    .GroupBy(x => x.StartDate)
    .FirstOrDefault()?.ToList() ?? new List<Course>();