运算符 >= 不能应用于字符串和日期时间类型的操作数

Operator >= cannot be applied to operands of type string and datetime

用户在url中输入两个参数,即开始日期和结束日期,它们以yyyyMMddhhmm格式作为字符串输入。我正在尝试获取这些字符串并将它们转换为日期,以便查询我的数据库。

[ResponseType(typeof(Detail))]
public IHttpActionResult GetDetail(string StartDate, string EndDate)
{
    DateTime StartDateTime;
    DateTime EndDateTime;

    StartDateTime = new DateTime();
    EndDateTime = new DateTime();

    StartDateTime = DateTime.ParseExact(StartDate, "yyyyMMddhhmm", null);
    EndDateTime = DateTime.ParseExact(EndDate, "yyyyMMddhhmm", null);

    var detail = from a in db.Details where (a.callDate >= StartDateTime && a.callDate <= EndDateTime) select a;

    var Response = new DetailResponse() { status = true, calls = detail };
    return Ok(response);
}

但是我得到的错误是 >= 不能用于日期时间和字符串。

编辑: 为了其中一个答案,我包括了一个模型 class 我用来显示数据。

DetailResponse.cs

public class DetailResponse
{
    public bool status { get; set; }
    public string statusMessage { get; set; }
    public IQueryable<Detail> calls { get; set; }
}

可能会发生这种情况,因为 callDate 是一个字符串。所以你不能将字符串与日期时间进行比较。这个问题的解决方案是拥有相同的类型。话虽如此,我会将 a.callDate 转换为 DateTime.

但是,我认为您最好在数据库级别更改 callDate 的数据类型。毫无疑问,这是个人意见。所以你不必遵循它。这样做您的代码将不需要任何更改。

现在,就代码而言,我上面建议的解决方案如下:

var allDetails = db.Details.AsEnumerable();
var details = from detail in details
              let callDate = DateTime.ParseExact(detail.callDate, "yyyyMMddhhmm", null)
              where callDate >= StartDateTime 
              && callDate <= EndDateTime
              select detail;

更新

正如我们在评论中总结的那样,我们必须调用 AsEnumerable,才能使上述查询正常工作。为什么需要这个?

借用 Jon Skeet 的话 Reimplementing Linq to Objects: Part 36 – AsEnumerable

Now it’s not entirely uncommon to want to perform some aspects of the query in the database, and then a bit more manipulation in .NET – particularly if there are aspects you basically can’t implement in LINQ to SQL (or whatever provider you’re using). For example, you may want to build a particular in-memory representation which isn’t really amenable to the provider’s model.

无法在数据库方法中正确翻译 DateTime.ParseExact

您的架构是什么样的? callDate 是字符串吗?在进行比较之前,您可能需要将 callDate 转换为 DateTime

var detail = from a in db.Details where (Convert.ToDateTime(a.callDate) >= StartDateTime &&  Convert.ToDateTime(a.callDate) <= EndDateTime) select a;

您的比较失败,因为您数据库中的日期是字符串类型,请尝试这样做:

[ResponseType(typeof(Detail))]
public IHttpActionResult GetDetail(string StartDate, string EndDate)
{
    DateTime StartDateTime = DateTime.ParseExact(StartDate, "yyyyMMddhhmm", null);
    DateTime EndDateTime = DateTime.ParseExact(EndDate, "yyyyMMddhhmm", null);

    var detail = from a in db.Details where (DateTime.ParseExact(a.callDate, "yyyyMMddhhmm", null) >= StartDateTime && 
                    DateTime.ParseExact(a.callDate, "yyyyMMddhhmm", null) <= EndDateTime) select a;
}

但是,您最好将 callDate 的类型更改为日期而不是 string

如前所述,您不能将字符串与 DateTime 进行比较,但是,鉴于日期格式为

yyyyMMddhhmm

(即年月日小时分钟),其中值都是数字,并且从变化最小 -> 变化最大,你可以安全地进行字符串比较:

var detail = from a in db.Details where (a.callDate >= StartDate && a.callDate <= EndDate) select a;

这是因为比较字符串时“201601010101”小于“201612312359”(同理"a"小于"b")。

这将节省您将数据转换为 DateTime

话虽如此,通过进行转换,您正在验证数据,如果格式不正确,可能会显示错误。