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


the user enters 2 parameters in url start date , end date , entered in format yyyymmddhhmm string. i'm attempting take these strings , turn them dates can query database.

[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 = in db.details (a.calldate >= startdatetime && a.calldate <= enddatetime) select a;      var response = new detailresponse() { status = true, calls = detail };     return ok(response); } 

however error >= can't used in datetime , strings.

edit: sake of 1 of answer i'm including model class i'm using display data.

detailresponse.cs

public class detailresponse {     public bool status { get; set; }     public string statusmessage { get; set; }     public iqueryable<detail> calls { get; set; } } 

probably happening, because calldate string. can't compare string datetime. solution problem have same type. being said convert a.calldate datetime.

however, think better change data type of calldate in database level. undoubtedly, personal opinion. don't have follow it. doing code not need change.

now, in terms of code solution suggested above following:

var alldetails = db.details.asenumerable(); var details = detail in details               let calldate = datetime.parseexact(detail.calldate, "yyyymmddhhmm", null)               calldate >= startdatetime                && calldate <= enddatetime               select detail; 

update

as concluded in comments, had call asenumerable, in order above query work. why needed?

borrowing jon skeet's words reimplementing linq objects: part 36 – asenumerable

now it’s not entirely uncommon want perform aspects of query in database, , bit more manipulation in .net – particularly if there aspects can’t implement in linq sql (or whatever provider you’re using). example, may want build particular in-memory representation isn’t amenable provider’s model.

the datetime.parseexact cannot translated in database method.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -