c# - Unable to cast object of type 'WhereListIterator`1[Web.Model.Appointment]' to type 'Web.Model.Appointment' -


this code fragment controller

[httppost] public actionresult cancel(string id,formcollection collection) {    //this how declare appt cannot seem correctly pass id method    //var appt = application.session.getobjectfromoid<appointment>(new objectid(id));     //i trying way instead error    var appt = (appointment)application.appointments.where(a=>a.id.equals(collection["id"]));    ..... } 

this error get:unable cast object of type 'wherelistiterator`1[web.model.appointment]' type 'web.model.appointment'.

this view:

<input type="button" value="save" onclick="updatecancel(); return false;" /><button>save</button> 

and function

 function updatecancel() {     $('#cancel').ajaxsubmit({      });     } 

so why getting error? or there way pass model.data.id function can use id instead?

the where-clause returns iterator. select want use instead. i.e., if expect 1 result, can use firstordefault. can use index, [1].

var appt = (appointment)application.appointments.where      (a=>a.id.equals(collection["id"]))      .firstordefault(); 

note firstordefault returns null when item not found, sure check result value.

ps: don't need cast there. without (appointment, error in title, should work, because items in list of type appointment.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -