c# - Check to see if row exists before adding it? -
i have certification process. right add people certification table if certified item in question.
on view page, have procedureid
, there active directory username. pass controller method adds them table.
however if exist don't need add them update certification date , leave else alone.
what having trouble checking if exist.
[httppost] public actionresult addcertification(int procedureid, formcollection collection) { string[] certifiedusers = collection["members"].split(','); ipacs_certification ipacs_certification = new ipacs_certification(); foreach (var item in certifiedusers) { // certification exist? ipacs_certification doesexist = db.ipacs_certification.where(m => m.adusername == item && m.procedureid == procedureid).select(m => m); ipacs_certification.procedureid = procedureid; ipacs_certification.certifieddate = datetime.now; ipacs_certification.adusername = item; db.ipacs_certification.add(ipacs_certification); db.savechanges(); } return redirecttoaction("addcertification"); }
my table contains following columns.
certid (key identity)
procedureid (fkey procedure table)
certifieddate datetime
adusername nvarchar
if exist should able grab row adusername , procedureid , grab unique single record. if exist need update certifieddate todays date. if record not found need perform above add.
the above code has error:
ipacs_certification doesexist = db.ipacs_certification.where(m => m.adusername == item && m.procedureid == procedureid).select(m => m);
cannot implicitly convert type system.linq.iqueryable ipacs_certification. know missing there.
can check if entry exist following way:
bool exists = db.ipacs_certification.firstordefault(m => m.adusername == item && m.procedureid == procedureid) != null;
if exist , want use data row rather way:
ipacs_certification certification= db.ipacs_certification.firstordefault(m => m.adusername == item && m.procedureid == procedureid); if(certification != null) { // exists, stuff data object } else { //doesn't exist, save certification }
Comments
Post a Comment