c# - Checking if an object exists in an SQL table based on a Column and some object's Field -
i trying check if there exists instance of object unique id in sql table. not place duplicates. here sample of have:
string ui = someobj.getuniqueid(); iqueryable<string> checkdataquery = cdq in db.someobjects cdq.uniqueid == ui select cdq.uniqueid; if (checkdataquery != ui) // attempt @ making sure // query returned , not // string representing nothing found, if there // better checking method more helpful.
the main issue running can not access checkdataquery
string
. attempted casting it, , using .single()/.first()
methods, however, former had no success, , latter made single string being returned list of characters.
you can check result enumerable.any
like:
bool ifexists = db.someobjects.any(r=> r.uniqueid == ui);
if want object, can use firstordefault
, check null
like:
var dbobject = db.someobjects.firstordefault(r=> r.uniqueid == ui); if(dbobject != null) { //record exists }
Comments
Post a Comment