Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Get Entity by Attribute value from EntityCollection with LINQ

Posted on by 417

Hello experts,

is there maybe a simple Linq equivalent to that?

Guid incidentid = (Guid)dataReader.GetValue(0); // just to let you know what this variable is

foreach(Entity incident in crmResult.Entities)
{
    if(incident.GetAttributeValue<EntityReference>("incidentid").Id == incidentid)
    {
        //Found! Do stuff here...         
    }
}


What I tried



if(crmResult.Entities.Where(x => x.GetAttributeValue<EntityReference>("incidentid").Id == incidentid))



And


Entity incident = from x in crmResult.Entities
where x.GetAttributeValue<EntityReference>("incidentid").Id == incidentid
select x;


And some other things but my knowledge in linq ends with FirstOrDefault.

*This post is locked for comments

  • bernhards Profile Picture
    bernhards 417 on at
    RE: Get Entity by Attribute value from EntityCollection with LINQ

    Hi Radu,

    sorry for the late response. Both of your solutions worked perfect.

    Thanks,

    Bernhard

  • Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Get Entity by Attribute value from EntityCollection with LINQ

    good luck :)

    I also get confused from time to time :)

    Let me know if you need further help.

    Regards,

    Radu

  • bernhards Profile Picture
    bernhards 417 on at
    RE: Get Entity by Attribute value from EntityCollection with LINQ

    Sorry, I got confused by all the articles I've read about LinQ. Your first solution already answered my question. I will try the code from your 2nd answer on monday out. (When I am back in office.)

    Have a great weekend and thanks a lot,

    Bernhard

  • Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Get Entity by Attribute value from EntityCollection with LINQ

    so you basically want to find the first instance of an incident in a collection that matches a certain criteria ?

    can you try:

    var found =crmResult.Entities.Where(x => x.GetAttributeValue<EntityReference>("incidentid").Id == incidentid).FirstOrDefault();

    Also, if you want the IndefOf method you can convert the IEnumerable to List by calling ToList() method and and the List<T> has the IndefOff method - see docs.microsoft.com/.../system.collections.generic.list-1.indexof

    Hope this helps,

    Radu

  • bernhards Profile Picture
    bernhards 417 on at
    RE: Get Entity by Attribute value from EntityCollection with LINQ

    Hi Radu,

    thanks for your explanation and hint. I searched if it is possible to get out the index of the IEnumerable and found that:

    https://stackoverflow.com/a/2471611/3772108

    My idea was to do something like

    Entity foundEntity = (Entity)crmResult.Entities[crmResult.Entities.Select((v, i) => new { value = v, index = i }).First(v => v.value.GetAttributeValue<Guid>("incidentid") == incidentid).index];

    if(foundEntity != null) {//Code here..}

    but the linq returns

    System.InvalidOperationException: 'Sequence contains no matching element'


    If I change the "First" to "Where" I have no access to ".index". When I change "First" to "FirstOrDefault" it throws the exception

    System.NullReferenceException: 'Object reference not set to an instance of an object.'


    Maybe it is really better to stay with the much simpler foreach..

    Thanks,

    Bernhard

  • Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Get Entity by Attribute value from EntityCollection with LINQ

    Hi Bernhard,

    The Where method returns an IEnumerable<T>, please see docs.microsoft.com/.../system.linq.enumerable.where

    You can't use this within an if statements, as the if statement expects a boolean to evaluate.

    If you want to filter out records from a collection based on a filter criteria you can do following

    var filteredIncidents = crmResult.Entities.Where(x => x.GetAttributeValue<EntityReference>("incidentid").Id == incidentid)

    Afterwards to apply logic against them, you need to iterate again through them and apply the logic

    foreach(var incident in filteredIncidents)

    {

    //logic here

    }

    Hope this helps,

    Radu

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans