c# - Error comparing guid to guid in linq -


the first if condition runs fine, second fails 'cannot cast guid string' error. compiler tells me both ids guids. so, why failing on 2nd 'if'? l.p.id guid, testp.id guid. don't see how there's failure here.

if (context.getset<pbm>().firstordefault(l=>l.p.id == testp.id) != null) {     context.getset<pbm>()            .remove(context.getset<pbm>()            .firstordefault(l => l.p.id == testp.id));     context.savechanges(); }  if (context.getset<moah>().firstordefault(l=>l.p.id == testp.id) != null) {     context.getset<moah>()            .remove(context.getset<moah>()            .firstordefault(l => l.p.id == testp.id));     context.savechanges(); } 

after using .any(l=>l.p.id == testp.id) suggested, unit tests fail exception system.invalidcastexception : invalid cast 'system.string' 'system.guid'.

error stack trace requested:

system.invalidcastexception : invalid cast 'system.string' 'system.guid'.    @ system.convert.defaulttotype(iconvertible value, type targettype, iformatprovider provider)    @ system.convert.changetype(object value, type conversiontype, iformatprovider provider)    @ mysql.data.entity.efmysqldatareader.changetype(object sourcevalue, type targettype)    @ mysql.data.entity.efmysqldatareader.getvalue(int32 ordinal)    @ system.data.entity.core.common.internal.materialization.shaper.errorhandlingvaluereader`1.getvalue(dbdatareader reader, int32 ordinal)    @ system.data.entity.core.common.internal.materialization.shaper.getcolumnvaluewitherrorhandling(int32 ordinal)    @ lambda_method(closure, shaper)    @ system.data.entity.core.common.internal.materialization.coordinator`1.readnextelement(shaper shaper)    @ system.data.entity.core.common.internal.materialization.shaper`1.simpleenumerator.movenext()    @ system.linq.enumerable.firstordefault(ienumerable`1 source)    @ system.linq.queryable.firstordefault(iqueryable`1 source, expression`1 predicate) 

try using guid.parse(string guid) static method.

cast string guid using linqpad

var pid=guid.parse(testp.id); if (context.getset<pbm>().any(l=>l.p.id == pid) ) {     context.getset<pbm>()            .remove(context.getset<pbm>()            .firstordefault(l => l.p.id == pid));     context.savechanges(); }  if (context.getset<moah>().any(l=>l.p.id == pid)) {     context.getset<moah>()            .remove(context.getset<moah>()            .firstordefault(l => l.p.id == pid));     context.savechanges(); } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -