c# - Linq To Entities Query -
c# - Linq To Entities Query -
consider next query :
var profilelst = ( in dbcontext.prospectprofiles i.createid == currentuser select new prospectprofile { prospectid = i.prospectid, live = i.live, name = i.name, serviceeta = i.opportunities.orderbydescending(t => t.followupdate) .firstordefault() .serviceeta.tostring(), followupdate = i.opportunities.orderbydescending(t => t.followupdate) .firstordefault() .followupdate } ) .tolist(); homecoming profilelst.orderbydescending(c=>c.followupdate) .skip(0).take(endindex) .tolist(); here in query please take @ followupdate , servicetype, these both have fetched chance table, there other work around these both..
one many relationship in tables like: prospectprofile -> opportunities
whether query have written ok or there work around can done in easier way.
the thing can improve avoid ordering twice changing code this:
var profilelst = dbcontext.prospectprofiles .where(i => i.createid == currentuser) .select(i => { var chance = i.opportunities .orderbydescending(t => t.followupdate) .first(); homecoming new prospectprofile { prospectid = i.prospectid, live = i.live, name = i.name, serviceeta = opportunity.serviceeta.tostring(), followupdate = opportunity.followupdate } }).tolist(); homecoming profilelst.orderbydescending(c => c.followupdate).take(endindex).tolist(); i made several changes original query:
i changed utilize method chains syntax. much easier read in opinion. i removed unnecessaryskip(0). the biggest alter in select part: i changed firstordefault first, because accessing properties of homecoming value anyway. throw descriptive exception if no chance exists. that's improve had: in case throw nullreferenceexception. that's bad, nullreferenceexceptions indicate bug in programme , not descriptive @ all. i moved part selects chance out of initializer, need sorting 1 time instead of twice. c# asp.net entity-framework linq-to-entities
Comments
Post a Comment