如何知道日期时间是否在 Entity Framework Core 中的两个日期之间

How to know if a DateTime is Between two Dates in Entity Framework Core

我有一个由 Entity Framework 核心创建的应用程序和一个数据库,我有一个名为 ListUsers 的实体,其中 属性:

public DateTime CreationDate { get; set; }

我有另一个 class 具有过滤器的属性,这是我的过滤器 class:

 public string Neighborhood { get; set; }
 public DateTime? InitDate { get; set; }
 public DateTime? EndDate { get; set; }

我有另一个属性来执行过滤器,其他属性工作正常但是当我发送 DateTime 属性时它不起作用,这是我的 Linq 查询来执行过滤器:

var result = await _context.ListUsers
                           .Include(lu => lu.List)
                           .ThenInclude(u => u.User)
                           .Where(lu => lu.CreationDate.Date >= filter.InitDate
                                        && lu.CreationDate.Date <= filter.EndDate
                                        && lu.Country == filter.Country
                                        && lu.City == filter.City
                                        && lu.Locality == filter.Locality
                                        && lu.Neighborhood == filter.Neighborhood
                                        && lu.List.Id == filter.ListId)
                           .ToListAsync();
return result;

在我的数据库中有两个带有此日期的寄存器:“2021-08-01 17:47:12.8614628”但“结果”始终为空,我从邮递员发送此 json :

{
   "listId": "090A5C4D-04AB-434E-A0EF-C579B6D1D0C8",  
   "initDate": "2021-08-01",
   "endDate": "2021-08-12",
   "neighborhood": "portsland"
}

任何可能的解决方案?

您过滤国家、城市和地区,但 JSON 不包含任何这些,因此过滤发生在 null 值上,这很可能是您没有得到的原因结果。

您需要提供 JSON 中的所有值,或者将过滤更改为对 null 值不执行任何操作。

示例过滤器:

&& (filter.Country == null || lu.Country == filter.Country)