web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Handle null entityreference in LINQ

(0) ShareShare
ReportReport
Posted on by

How to handle join when the EntityReference is null. For example 

var info = (from cr in xrm.CreateQuery<new_course>()
join c in xrm.CreateQuery<Contact>()
on cr.course_contact.Id equals c.ContactId
where c.Id == courseGuid
select cr.title).FirstOrDefault();

when course_contact is null C# throws null exception 

Microsoft.Crm.CrmArgumentNullException: attributeName ---> Microsoft.Crm.CrmArgumentNullException: attributeName ---> System.ArgumentNullException: Value cannot be null.
Parameter name: attributeName
--- End of inner exception stack trace ---
at Microsoft.Crm.Metadata.EntityMetadata.TryGetAttribute(String attributeName, NameMappingType mapping)
at Microsoft.Crm.Metadata.EntityMetadata.GetAttribute(String attributeName, NameMappingType mapping)
at Microsoft.Crm.Query.EntityExpression.FromSdkQueryVisitor.Visit(ConditionExpression condition)
at Microsoft.Crm.Query.EntityExpression.FromSdkQueryVisitor.Visit(FilterExpression filter)
at Microsoft.Crm.Query.EntityExpression.FromSdkQueryVisitor.Visit(QueryExpression query)
at Microsoft.Crm.Query.EntityExpression.InitializeFromQuery(QueryBase query)
at Microsoft.Crm.Query.EntityExpression.Converter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at Microsoft.Crm.BusinessEntities.ConversionHelpers.Convert(ICrmConversionContext conversionContext, Object source, Type destinationType)
at Microsoft.Crm.Extensibility.DictionaryMapper.Map(ParameterCollection inputs, ICrmConversionContext context)
at Microsoft.Crm.Extensibility.InternalOperationPlugin.Execute(IServiceProvider serviceProvider)
at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
--- End of inner exception stack trace ---
at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.PipelineInstrumentationHelper.Execute(Boolean instrumentationEnabled, String stopwatchName, ExecuteWithInstrumentation action, PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.Pipeline.<>c__DisplayClass2_1.<Execute>b__0()

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    Try with this-

    ===========

    var info = (from cr in xrm.CreateQuery<new_course>()

                                   join c in xrm.CreateQuery<Contact>()

                                   on cr.course_contact != null

                                   && cr.course_contact.Id equals c.ContactId

                                   where c.Id == courseGuid

                                   select cr.title).FirstOrDefault();

    ==============

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hi Ravi,

    We cannot use multi-condition join using && however, I have tried using left join to check course_contact is not null as below

                   var info = (from cor in xrm.CreateQuery<new_course>()

                              join con in xrm.CreateQuery<Contact>()

                                  on new { a = cor.course_contact != null, contactId = cor.course_contact.Id }

                                 equals new { a = true, contactId = con.Id } into courseContact

                              from cc in courseContact.DefaultIfEmpty()

                              select cor.title

                              ).FirstOrDefault();

    but I am getting the error below

    Invalid 'join' condition. An entity member is invoking an invalid property or method.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Sorry, I didn't have any project to cross check however I tried the SDK sample and tried the similar query. You can put the null check in where clause.

    Try this

    ================

    var info = (from cr in xrm.CreateQuery<new_course>()

                                       join c in xrm.CreateQuery<Contact>()

                                       on  cr.course_contact.Id equals c.ContactId

                                       where cr.course_contact != null

                                       && c.Id == courseGuid

    =============================

    Hope this helps.

  • Community Member Profile Picture
    on at

    Tried already. Didn't work :(

  • Sid07 Profile Picture
    285 on at

    I have seen this error before. u can try below change and update me if it worked or not

    var info = (from cr in xrm.CreateQuery<new_course>()

    join c in xrm.CreateQuery<Contact>()

    on cr.GetAttributeValue<EntityReference>("course_contact").Id equals c.GetAttributeValue<EntityReference>("contactid").Id

    where c.GetAttributeValue<Guid>("contactid").Id == courseGuid

    select cr.title).FirstOrDefault();

    Also please check if course_contact is right name of the field ?

  • Community Member Profile Picture
    on at

    Hi Siddesh

    It's the same error as mentioned in my first post i.e. Microsoft.Crm.CrmArgumentNullException: attributeName...

    I think query result or exception should be the same whether we will use early or late bound.

  • Suggested answer
    Yagasoft Profile Picture
    240 on at

    Think of the query in terms of FetchXML; so compare directly with the lookup itself:

    var info = (from cr in xrm.CreateQuery<new_course>()

     join c in xrm.CreateQuery<Contact>()

       on cr.course_contact equals c.ContactId

     where c.Id == courseGuid

     select cr.title).FirstOrDefault();

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Can you try below code.

    I have changed the order - on c.ContactId equals cr.course_contact.Id

    var info = (from cr in xrm.CreateQuery<new_course>()

    join c in xrm.CreateQuery<Contact>()

    on c.ContactId equals cr.course_contact.Id

    where c.Id == courseGuid

    select cr.title).FirstOrDefault();

    See below link for more complex linq queries.

    https://github.com/microsoft/PowerApps-Samples/blob/master/cds/orgsvc/C%23/QueriesUsingLINQ/ComplexQueries/SampleProgram.cs

    If found helpful, Please mark my answer verified.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans