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)

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

I have the same question (0)
  • Shidin Haridas Profile Picture
    3,497 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

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