“Value cannot be null.Parameter name: attributeName” when running Linq query with join

If you run the following query using the CRM 2011 Linq provider (I'm using Linq pad here):

from u in SystemUserSet
where u.Id == new Guid("f19f4c09-965b-e011-ab82-000c2957f385")
select u

All is well. However, if you add a join:

from u in SystemUserSet
join t in TerritorySet
on u.TerritoryId.Id equals t.TerritoryId.Value
where u.Id == new Guid("f19f4c09-965b-e011-ab82-000c2957f385")
select u

You will get the following exception:

System.ArgumentNullException: Value cannot be null.
Parameter name: attributeName

Looking at the Query Expression created, you will see:

<ConditionExpression>
       <AttributeName i:nil="true" />
        <Operator>Equal</Operator>
        <Values xmlns:d5p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
          <d5p1:anyType xmlns:d6p1="http://schemas.microsoft.com/2003/10/Serialization/" i:type="d6p1:guid">f19f4c09-965b-e011-ab82-000c2957f385</d5p1:anyType>
        </Values>
      </ConditionExpression>

The problem seems to do with the way that the Linq provider resolves attribute names on the Id field.

You need to change the query to be (specifying the Id parameter explicitly):

from u in SystemUserSet
join t in TerritorySet
on u.TerritoryId.Id equals t.TerritoryId.Value
where u.SystemUserId == new Guid("f19f4c09-965b-e011-ab82-000c2957f385")
select u

Pingbacks and trackbacks (3)+

Comments are closed