Bactos
Bactos

Reputation: 1243

CRM 2011 Custom Workflow Linq Queries providing null values when they are not null

I am writing a Custom Workflow for MS Dynamics CRM 2011. In my workflow I have a class that I use specifically for queries – this is because I do a significant number of quires for other methods within the workflow. For my queries I am using LINQ.

I ran into an issue in my testing where I have two methods that go out and get a different option set values. They both work if tested individually. However, if I test them back to back … get this option set value…then get this option set value: The subsequent query always returns a null.

<!-- language: cs -->
public int GetOptionSetValues(WorkFlowHelper workFlowHelper, String bracketId)
{
     Guid _bracketId = workFlowHelper.GuidChanger(bracketId);
     var query = from b in workFlowHelper.serviceContext.myEntitySet
                 where b.myEntitySetId.Equals(_bracketId)
                 select new { b.itemToGetOptionSetFrom };
     foreach (var qin query )
     {
       if (q.itemToGetOptionSetFrom == null)
       {
        return 0;
       }
       else
       {
        int optionSetValue = q.itemToGetOptionSetFrom;
        return optionSetValue;
       }
     }
      return 0;
 }

Both methods are the same as above with the exception of "itemToGetOptionSetFrom" is different. I have checked the DB and the items do infact have values.

Can anyone explain why this is doing this? Or point me in the right direction to correct this issue? Thanks,

Upvotes: 2

Views: 782

Answers (1)

Daryl
Daryl

Reputation: 18895

I'm guessing you're retrieving the same object with the same service context. If you request an attribute(s) from an entity, the CRMContext will cache a copy of that entity with only those fields and the id. This means all subsequent LINQ requests that include that entity will return the cached copy of that entity that ONLY includes the id and attributes you previously requested. To avoid this you can call ClearChanges() to remove the cached version before making your next request.

Upvotes: 4

Related Questions