首页 > 解决方案 > 参数 2:不能从 'bool' 转换为 'System.DateTime?'

问题描述

我尝试在两个日期之间创建一个数据范围:开始和结束。

为此,我有以下查询:

public DateTime CreatedAt { get; set; }

private IQueryable<Alert> GetAlerts(int organisationId, DateTime? beginDate = null, DateTime? endDate = null,  bool includeDone = false)
{
    var query = 
        _patientDbContext.Alerts
            .Where(i => i.OrganisationId == organisationId && i.CreatedAt = endDate.ToString() <= beginDate.ToString());

    if (!includeDone)
    {
       query = query.Where(i => !i.IsDone);
    }

    query = query.OrderBy(i => i.Deadline);

    return query;
}

但我收到此错误:

参数 2:不能从 'bool' 转换为 'System.DateTime?'

我究竟做错了什么?

标签: c#linqtype-conversion

解决方案


这是因为您的子句中有一个赋值(=符号)Where

i.CreatedAt = endDate.ToString() <= beginDate.ToString()

你想要做的是这个,假设CreatedAtDateTime

var query = _patientDbContext.Alerts.Where(i =>
    i.OrganisationId == organisationId &&
    (beginDate == null || i.CreatedAt => beginDate.Value) &&
    (endDate == null || i.CreatedAt <= endDate.Value)
);

推荐阅读