asp.net mvc - MVC4 Linq Database Call -


working on first mvc app. using simple membership database comes mvc4 manage users , roles. understanding having 3 steps find() call below work: simple membership guid of person logged in, use simple membership guid id in couriers database, use id missing parameter in find() call below return model / record edit in view?

how / use parameter .find() call? on right track, or making lot more complicated?

ps - saving membership guid in couriers database in separate column courier id primary key column when courier record created. thank you.

   public actionresult editcourier()     {         system.web.security.membershipuser user = membership.getuser();         var provideruserkey = user.provideruserkey;         guid userid = (guid)provideruserkey;         string useridstring = userid.tostring();          var model =             c in db.couriers             c.couriermembershipid == useridstring             select c.id;          courier courier = db.couriers.find();          if (courier == null)         {             return httpnotfound();         }         return view(courier);     } 

msdn documentation says:

uses primary key value attempt find entity tracked context. if entity not in context query executed , evaluated against data in data source, , null returned if entity not found in context or in data source.

so if have userid primary key in couriers table can use .find() this:

courier courier = db.couriers.find(userid); 

otherwise should use standard linq query , call .firstordefault() or singleordefault() method:

courier courier = db.couriers.firstordefault(c=>c.couriermembershipid == useridstring); 

edit:

i suppose getting error next query:

var model =             c in db.couriers             c.couriermembershipid == useridstring             select c.id; 

the reason getting query ienumerable<courier> model. making query single courier object try call .firstordefault() @ end:

var model =    (from c in db.couriers                 c.couriermembershipid == useridstring                 select c.id).firstordefault(); 

Comments