Fixed – The given key was not present in the dictionary error while using FormattedValues in LINQ in CRM 2013.

Hi,

I was getting the following exception while trying to get the text for an option set field named area of law in linq.

The query with joins


var authorization = (from a in
SDKHelper.XrmServiceContext.lss_AuthorizationSet 
join c in SDKHelper.XrmServiceContext.lss_contractSet on a.lss_ContractId.Id equals c.Id

join l in

SDKHelper.XrmServiceContext.lss_lawyerSet on c.lss_LawyerId.Id equals l.Id

where a.lss_AuthID == authorizationId

select
new
Authorization
{

AreaOfLawAndContractType = c.lss_Area_of_Law != null? c.FormattedValues[“lss_area_of_law”] : default(string)

}).FirstOrDefault();

return authorization;

 

The fix was instead of using FormattedValues collection of related entity in join, we need to use the FormattedValues collection of the primary entity.

 

var authorization = (from a in
SDKHelper.XrmServiceContext.lss_AuthorizationSet

join c in
SDKHelper.XrmServiceContext.lss_contractSet on a.lss_ContractId.Id equals c.Id

join l in
SDKHelper.XrmServiceContext.lss_lawyerSet on c.lss_LawyerId.Id equals l.Id

where a.lss_AuthID == authorizationId

select
new
Authorization

{

AreaOfLawAndContractType = c.lss_Area_of_Law != null? a.FormattedValues[“lss_area_of_law”] : default(string)

}).FirstOrDefault();


return authorization;

 

Hope it helps.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s