Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to do Count(), Group with QueryExpression/ObservableCollection Result with ASP.NET?

(0) ShareShare
ReportReport
Posted on by

In the original version of this ASP.NET program which is for processing data from Dynamic CRM 365, the QueryExpression Result is transferred to ObservableCollection, then final result in xml format. Now I need to add some count() and group by to the QueryExpression or ObservableCollection. How can I do it? May be with LINQ? Thanks.

*This post is locked for comments

  • gdas Profile Picture
    50,091 Moderator on at
    RE: How to do Count(), Group with QueryExpression/ObservableCollection Result with ASP.NET?

    Hi ,

    Please find below sample code  - hope this helps.

    --------------------------------------------------------------------------------------------------

                // get data using query expression
    EntityCollection ec = Service.RetrieveMultiple(qe); // Transfer Entity colletion to Custom list of Object List<Observable> ObservableCollection = new List<ObservableCollection>(); EntityCollection ec = Service.RetrieveMultiple(query); foreach (var targetEntity in ec.Entities) { Observable _observable = new ObservableCollection(); //Initialize the object _observable.Name = (targetEntity.Contains("name")) ? targetEntity["name"].ToString() : ""; // Let's assume there have one name property in the entity collection ObservableCollection.Add(_observable); // Adding object to the ObservableCollection } // Count group by sample - using Name attribute var q = from observ in ObservableCollection group observ by observ.Name into g select new { g.Key, Count = g.Count() };

    ---------------------------------------------------------------------------------------------

          //Sample Observable class 

        public class Observable
        {
            public string Name { get; set; }
    
        }

    
                          
  • Shidin Haridas Profile Picture
    3,497 on at
    RE: How to do Count(), Group with QueryExpression/ObservableCollection Result with ASP.NET?

    convert the Query Expression into a list as follows:

    var results=_crmOrgService.RetrieveMultiple(queryExpression).Entities.ToList();

    Once you have your familiar list form, you can do the following operations using LINQ.
    For eg:,

    1. Count

    int recordsCount=results.Count()

    2. Filter and get only the ones with name='Tom'

    int recordsCount=results.Where(n=>n.GetAttributeValue<string>("name")=="Tom");

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