0
votes

I try to compare 2 Guid in Linq to Crm request and it's not working. I don't see why:

Guid IdThematique = new Guid(ddlThematique);
Sollicitations = Sollicitations.Where(i => i.Sollicitation.SubjectId.Id == IdThematique);

This is the catched:

System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet. à XXX dans XXX 74 à System.Linq.Enumerable.WhereListIterator1.MoveNext() à System.Linq.Buffer1..ctor(IEnumerable1 source) à System.Linq.OrderedEnumerable1.d__0.MoveNext() à System.Linq.Enumerable.Count[TSource](IEnumerable`1 source) à xxx dans xxx 200

Thank you

3
I don't suppose you have an English translation?James Wood
at least it's not in arabic ;)Alex Gordon

3 Answers

0
votes

I'll agree with Mario, but I'd make it an extension method:

More concise version:

public static class EntityReferenceExtensions{
    public static Guid GetIdOrDefault(this EntityReference entity){
        return (entity ?? new EntityReference()).Id;
    }
}

Arguably more readable version:

public static class EntityReferenceExtensions{
    public static Guid GetIdOrDefault(this EntityReference entity){
        if(entity == null){
            return Guid.Empty;
        } else {
            return entity.Id;
        }
    }
}

But with the extension method, you could handle any Entity Reference of any Entity object (early or late bound)

Sollicitations.Where(i => (i.Sollicitation.SubjectId.GetIdOrDefault() == IdThematique))

Although now that I think about it, I don't think that this works for linq to CRM. Just Linq to Objects.

3
votes

Possibly you have records where Subject is not filled and i.Sollicitation.SubjectId is null.

From top of my head, something below should make it work:

Sollicitations.Where(i => (i.Sollicitation.SubjectId != null && i.Sollicitation.SubjectId.Id == IdThematique))
-1
votes

You are converting ddlThematique to an Guid which is wrong I think. You need to use the Text property from the ddlThematique.