Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to convert retrieved records to list and group

Posted on by 4,440

Hi 

After retrieving records how do i convert to list and group. Basically i am retrieving records want to group by owners. 

foreach (Entity ent in EC.Entities)
{
string subject = ent["name"].ToString();
string SR_Number = ent["number"].ToString();
EntityReference originator = (EntityReference)ent.Attributes["originator"];
string originatorname = originator.Name;
EntityReference ownerid = (EntityReference)ent.Attributes["ownerid"];
string ownername = ownerid.Name;

}

*This post is locked for comments

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: How to convert retrieved records to list and group

    Hi Krish,

    In order to convert these retrieved records to a list, just try the code below.

    EC.Entities.ToList()

    For grouping, this by owner, try the LINQ query below.

    var linqQuery2 = (from ent in EC.Entities.ToList()
        group ent by ent.OwnerId into myGroup
        select new
        {
            OwnerName = myGroup.First().OwnerName,
            OrderCount = myGroup.Count()
        });

    See: http://stackoverflow.com/questions/7642365/using-group-in-linq-query

    Hope this helps.

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to convert retrieved records to list and group

    Hi Krish,

     EC.Entities.ToList() would give you a list.

     For grouping.. Have a look here:

      stackoverflow.com/.../using-linq-to-group-a-list-of-objects-into-a-new-grouped-list-of-list-of-objects

     I think it would translate to this:

    var groupedByOwnerId = EC.Entities.ToList().GroupBy(e =>

                                          ((EntityReference)e["ownerid"]).Id)

                                           .Select(grp => grp.ToList())

                                           .ToList();

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans