Linq Greater and Less Than operator in a string containing a date

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

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

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_startis a line, we cannot change it due to complications elsewhere. Anyway, around this problem?

+4
source share
4 answers

p.fixture_start - , , :

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

fixture_start, DateTime, :

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

?

0

, , Linq to Entities , Linq to Objects, string DateTime:

var results = Mapper.Map<IEnumerable<VesselViewModel>>(db.tbl_vessels
             .Where(p => 
                   (p.vessel_type.Contains("AHTS")) &&
                   (p.spotlist_id == 2))
             .AsEnumerable()// This should make the trick
             .Where(p => DateTime.Parse(p.fixture_start) <= System.DateTime.Now.Date)
             .ToList());
0

fixture_date . . , , yyyymmdd. , , . , .

p.fixture_date <= System.DateTime.Now.ToString("yyMd") // not sure this format is correct
0

Source: https://habr.com/ru/post/1628526/


All Articles