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
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; } }
convert the Query Expression into a list as follows:
Once you have your familiar list form, you can do the following operations using LINQ.
For eg:,
1. Count
2. Filter and get only the ones with name='Tom'
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
54
Victor Onyebuchi
6