I started experimenting with LINQ DynamicLibrary. I am trying to replace a group of LINQ statements with multiple or just one Dynamic LINQ query. My existing static query is as follows:
private List<TicketChartData> GenerateImpl(IList<ServiceItem> myList) { var output = from ticket in myList group ticket by ticket.Region into grouped orderby grouped.Count() descending select new TicketChartData(grouped.Key, grouped.Count(), grouped.ToList()); return output.ToList(); }
As you can see, my unit of work is ServiceItem . This works great and gives me a result set grouped by Region . Using DynamicLibrary, my attempt is to be able to group any valid dynamic field (I will handle any checks separately). So, I tried to write the same query using DynamicLibrary, but not very successful. Here is a new method that, of course, does not compile:
private List<TicketChartData> GenerateImpl(IList<ServiceItem> myList, string field) { IQueryable<ServiceItem> queryableList = myList.AsQueryable<ServiceItem>(); IQueryable groupedList = queryableList.GroupBy(field,"it"). OrderBy("Key descending"). Select("new (Key as Key)");
I was unable to extract from either Count or List from the group. How can I do it? I spent a lot of time finding a solution. If possible, I want to avoid using Reflection. I had some pointers on this front at the following link, but that did not help my real problem. Thanks in advance for any help.
System.LINQ.Dynamic: select ("new (...)") in the list <T> (or any other enumerated set <T>)
source share