Ling查询从两个日期之间获取所有值

Chr*_*mut 1 c# linq asp.net-mvc

是否可以使用linq创建查询以从两个用作分隔符的日期之间获取一系列值?

恩.

获取在12/12/12和12/12/13之间具有Date列值的所有实体.

我只是在寻找暗示或方法.

Jam*_*mes 7

当然你可以,没有"介于"语法,如果这是你得到的,但它是相对简单的做,例如

list.Where(x => x.Date >= StartDate && x.Date <= EndDate);
Run Code Online (Sandbox Code Playgroud)

如果你想要更好的可读性,你甚至可以为DateTimeie 编写扩展方法

public static class DateTimeHelpers
{
    public static bool Between(this DateTime dateTime, DateTime startDate, DateTime endDate)
    {
        return dateTime >= startDate && dateTime <= endDate;
    }
}
...
list.Where(x => x.Date.Between(StartDate, EndDate));
Run Code Online (Sandbox Code Playgroud)