How to use DateTimeOffset in a C# linq query to the database

1

I am using entity framework and AutoMapper in my project. In the database DatreTime fields were initially created as a string type fields and stays the same (example the fields 'DepartureTime' and 'ArrivalTime'). I am trying to make a DateTime comparison in my solution in a linq query to the dbContext by using DateTimeOffset in an effort to convert the string to a DateTime. But I get an error

The query I'm using is:

db.Passengers
.Where(p => p.AreaOfVisit == area && DateTimeOffset.Parse(p.DepartureTime).UtcDateTime > incidentDate &&
                            DateTimeOffset.Parse(p.ArrivalTime).UtcDateTime < incidentDate).ProjectTo<PassengerDTO>()
                .ToList();

I get this error message.

System.NotSupportedException HResult=0x80131515 Message=LINQ to Entities does not recognize the method 'System.DateTimeOffset Parse(System.String)' method, and this method cannot be translated into a store expression.

What can be the best way to solve this issue.

c#
linq
datetime
linq-to-sql
datetimeoffset
asked on Stack Overflow Sep 19, 2019 by sm101 • edited Sep 19, 2019 by Soner Gönül

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0