I was using LINQ to retrieve multiple rows and I am loving that but I was doing a linq query to get the different address for an account.
This worked fine except I wanted to check the address type by name. So I wanted to retrieve the Primary address. I found out that in LINQ, optionsetvalues only return the int number value and not text/label. To get the label you have to look it up using the metadata service. ARGHGHGHG how frustrating. I was searching OptionSetValue, metadata search and couldn’t find anything for ages until I saw that the samples has a couple of projects all about metadata calls.
You can find them here
it was annoying at the time because I had just figured out how to return the values in LINQ and I didn’t want to hard code the values to the int optionSet values because I thought it wouldn’t work if someone added in new address types but it wouldn’t still work if I used the address type string name.
Microsoft.Xrm.Sdk.Client.OrganizationServiceContext orgContext = new Microsoft.Xrm.Sdk.Client.OrganizationServiceContext(_service);
IEnumerable<CustomerAddress> addresses = from c in orgContext.CreateQuery<CustomerAddress>() join aa in orgContext.CreateQuery<Account>() on c.ParentId.Id equals aa.Id
where (c.ParentId.Id == new Guid(“92eb0d0e-b22e-e011-9645-00155d106b02”))
select c;
System.Console.WriteLine(“========================”);
foreach (CustomerAddress ca in addresses) {
System.Console.WriteLine(ca.AddressTypeCode + ” ”
+ ” ” + ca.CustomerAddressId
+ ” ” + ca.AddressTypeCode.Value
+ ” ” + ca.Line1
+ ” ” + ca.Line2
+ ” ” + ca.Line3);
OptionSetValue opp = ca.AddressTypeCode;
System.Console.WriteLine(” ” + ca.CustomerAddressId.Value);
}
It’s not just LINQ.
I’m executing a RetrieveMultiple request and I’m only getting the OptionSetValue integer code too.
The name of the selected Option is not returned.
LikeLike
Hi Ben,
Have you tried:
var query1 = from c in ContactSet
where (c.Id.Equals(“d9628e12-6bd3-e111-afc8-00165d00bb06”))
select new
{
AcctRole = c.AccountRoleCode,
ProviderType = c.carepro_ProviderType,
sta = c.StatusCode,
AcctRoleCode = c.FormattedValues[“accountrolecode”],
AddType = c.Address1_AddressTypeCode,
AddressTypeCode = c.FormattedValues[“address1_addresstypecode”],
ContId = c.Id
};
foreach (var c in query1)
{
System.Console.WriteLine(“AcctRole: ” + c.AcctRoleCode + ” | AddressTypeCode: ” + c.AddressTypeCode);
}
LikeLike
I have found out that you need to then retrieve the text values of the optionSet by getting the metadata.
There are a few examples in the SDK samplecode folder that you can use to help you.
it seems a rather odd way to do things
LikeLike
I guess it’s to do with multi-language support, but it would have been nicer if we just passed in the language code to use, rather than having to make a separate service call
LikeLike