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)

Get Entity by Attribute value from EntityCollection with LINQ

(0) ShareShare
ReportReport
Posted on by 423

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

I have the same question (0)
  • Radu Chiribelea Profile Picture
    6,667 on at

    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

  • bernhards Profile Picture
    423 on at

    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
    6,667 on at

    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
    423 on at

    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
    6,667 on at

    good luck :)

    I also get confused from time to time :)

    Let me know if you need further help.

    Regards,

    Radu

  • bernhards Profile Picture
    423 on at

    Hi Radu,

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

    Thanks,

    Bernhard

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