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);
}