Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

working with Entity Collection

(0) ShareShare
ReportReport
Posted on by 645

Hey I have entitycollection and I need to search it according to specific attributes (not Id) Is it possible? if not how can I convert entitycollection to a list ?

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: working with Entity Collection

    Hi , 

    You can use foreach to loop through the entity collection and add entity values to your Generic List like below . Although there is a complex way , but I have not tried at the moment .

           // Create your own Generic Class
           public class Account
            {
                public String Name { get; set; }
                public string Phone { get; set; }
    
            }

                // Declare the Generic List
    List<Account> listAccDetails = new List<Account>(); EntityCollection Result = service.RetrieveMultiple(query); foreach (var targetEntity in Result.Entities) {
    // You can also check here the attributes value and accordingly add to your generic list
    Account _account = new Account(); _account.Name = (targetEntity.Contains("name")) ? targetEntity["name"].ToString() : ""; _account.Phone = (targetEntity.Contains("phone")) ? targetEntity["phone"].ToString() : ""; listAccDetails.Add(_account); // Add to list }
    listAccDetails = listAccDetails.Where(p => p.Name == "test").ToList(); // Filter the list using Linq



  • Suggested answer
    Jeet Gandhi Profile Picture
    320 on at
    RE: working with Entity Collection

    Hello,

    You need to loop through the entities in the entitycollection as

    foreach(var entity in entitycollection.entities)

    {

    }

    For each of the entity record in the loop above, you can get the value of an attribute using "entity.GetAttributeValue("AttributeName")".

    To convert entitycollection to a list, use the code as below:

    List<entitycollection> entityCollectionListName = new List<entitycollection>();

    entityCollectionListName.AddRange(entitycollection.entities);

    Please mark it as answer if found helpful.

    Thank you,

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans