web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)
Active Discussion

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

  • Shidin Haridas Profile Picture
    3,499 on at

    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");
  • gdas Profile Picture
    50,091 Moderator on at

    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; }
    
        }

    
                          

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans