How to query (using LINQ) FormattedValues in Dynamics CRM 2011
c#, dynamics-crm-2011
Solution
You can only get access to the `_____Set` entities when generating the early-bound CRM file (look into `crmsvcutil.exe`/`Xrm.cs` online) and creating an early-bound derivative of `CrmOrganizationServiceContext` (commonly called `XrmServiceContext`). You can see the available constructors in the early-bound file.
So if you know the (int) value of the `OptionSetValue` in advance (7, in this case), you can just use this value as one of the arguments in the `Where` clause, as you've stated elsewhere:
.Where( c => c.new_AccreditationStatus.Value == 7)
Problem
I am using LINQ to retrieve Account type entities from Microsoft Dynamics CRM Online. I am unable to filter the list for a particular formatted value. I have the correct value, but am receiving zero records. I'm creating my connection like this: ``` var connection = new CrmConnection("CRMOnline"); connection.ProxyTypesEnabled = true; CrmOrganizationServiceContext _context = new CrmOrganizationServiceContext(connection); ``` I've tried: ``` List<Account> items = _context.CreateQuery<Account>() .Where( c => ((OptionSetValue)c["new_accreditationstatus"]).Equals(7)) .ToList(); ``` and ``` List<Account> items = _context.CreateQuery<Account>() .Where( c => c.GetFormattedAttributeValue("new_accreditationstatus") == "7" .ToList(); ``` and ``` List<Account> items = _context.CreateQuery<Account>() .Where( c => c["new_accreditationstatus"] == "7" .ToList(); ``` The last on throws a System.Format exception. Filters on normal properties, i.e. `.Where(c => c.AccountNumber.StartsWith("2010"))` work perfectly fine.