Linq大于和小于运算符对包含日期的字符串 [英] Linq Greater and Less Than operator on string containing date

查看:63
本文介绍了Linq大于和小于运算符对包含日期的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中编写了一个linq查询,我想对其使用小于"运算符.但是,我尝试将其应用于的列的类型为字符串(我们无法更改),并导致智能感知将错误抛出为<".运算符不能用于字符串类型.

I am writing a linq query in my application that I want to use a "less than" operator on. However column I am trying to apply it to is of type string (which we can't change) and is causing intellisense to throw an error as the "<" operator cannot be used on string type.

我的问题是我还能怎么做?这是我的代码中发生错误的部分.

My question is how else can I do this? Here is the part of my code that the error occurs on.

public ActionResult Index()
{
    var results = Mapper.Map<IEnumerable<VesselViewModel>>(db.tbl_vessels
    .Where(p => 
          (p.vessel_type.Contains("AHTS")) &&
          (p.spotlist_id == 2) &&
          (p.fixture_start <= System.DateTime.Now.Date)
     )
    .ToList());
    return View(results);
}

fixture_start是一个字符串,由于其他地方的复杂性,我们无法更改它.反正有这个问题吗?

fixture_start is a string, we can't change it due to complications elsewhere. Is there anyway around this problem?

推荐答案

如果p.fixture_start是包含日期的字符串,则必须在比较之前对其进行解析:

If p.fixture_start is a string containing a date, then you will have to parse it before compare:

(p => DateTime.Parse(p.fixture_start) <= System.DateTime.Now.Date)

这篇关于Linq大于和小于运算符对包含日期的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆