c# - MS Dynamics CRM System.InvalidCastException -
i have strange problem, have tons of entities , have xrmschema.cs generated crmsvcutil in crm system worked me pretty good, while using queries , converting them entity objects in code. convertion method worked perfect entites except one, keep getting system.invalidcastexception: unable cast object of type 'microsoft.xrm.sdk.entity' error 1 entity. here's following code segment generates it, throws exception in tolist() method:
public list<etel_productcharacteristic> retrievecharacteristic(guid characteristicid) { using (xrmdatacontext context = new xrmdatacontext(crmconnection.organizationservice)) { var query = characteristic in context.etel_productcharacteristicset characteristic.etel_productcharacteristicid == characteristicid && characteristic.statecode.value == etel_productcharacteristicstate.active select characteristic; return query.tolist(); } }
can point me might wrong code. way solutions read on on internet suggests use of enableproxy method. in case enableproxy method being called while organizationservice being initialized, not culprit. , other conversion work fine in plugin there has wrong one.
you need change line:
where characteristic.etel_productcharacteristicid == characteristicid && characteristic.statecode.value == etel_productcharacteristicstate.active
to read
where characteristic.etel_productcharacteristicid.id == characteristicid && characteristic.statecode.value == (int)etel_productcharacteristicstate.active
honestly, you'll need double-check me on (int)
cast, may not required - don't have code in front of me verify.
Comments
Post a Comment