c# - Linq To Entity does not support -
i learning mvc , storing user's date of birth show age. data saved properly. sort data followed this tutorial. works on first name , last name on sorting age throws exception only parameterless constructors , initializers supported in linq entities.
code action is
public actionresult index(string sortorder, int? page, string currentfilter, string q) { viewbag.currentsort = sortorder; viewbag.fnsortparam = string.isnullorwhitespace(sortorder) ? "fname_desc" : ""; viewbag.lnsortparam = sortorder == "lname" ? "ln_desc" : "lname"; viewbag.agesortparam = sortorder == "age" ? "age_desc" : "age"; var query = q; if (query != null) page = 1; else query = currentfilter; var users = db.myappusers.select(a => a); if (!string.isnullorwhitespace(query)) { var nquery = query.toupper(); users = users.where(a => a.fname.toupper().contains(nquery) || a.lname.toupper().contains(nquery) || a.email.contains(nquery)); } switch (sortorder) { case "fname_desc": users = users.orderbydescending(a => a.fname); break; case "lname": users = users.orderby(a => a.lname); break; case "ln_desc": users = users.orderbydescending(a => a.lname); break; case "age": users = users.orderby(a => new datetime(datetime.now.subtract(a.dob).ticks)); break; case "age_desc": users = users.orderbydescending(a => new datetime(datetime.now.subtract(a.dob).ticks)); break; default: users = users.orderby(a => a.fname); break; } // public datetime age //{ // { return new datetime(datetime.utcnow.subtract(dob).ticks); } //} int pagesize = 3; int pagenumber = (page ?? 1); return view(users.topagedlist(pagenumber, pagesize)); }
and view is
@foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.fname) </td> <td> @html.displayfor(modelitem => item.lname) </td> <td> @{ var age = new datetime(datetime.utcnow.subtract(item.dob).ticks); @html.displaytextfor(modelitem => age.year) } </td> <td> @html.displayfor(modelitem => item.email) </td> </tr> }
i can't figure out. can help?
Comments
Post a Comment