LINQ DynamicLibrary: how to extract score and list from IQueryable

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)"); // Not what i need return output.ToList(); } 

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>)

+4
source share
2 answers

For Count, it can be written as follows:

 private List GenerateImpl(IList myList, string field) { IQueryable queryableList = myList.AsQueryable(); IQueryable groupedList = queryableList.GroupBy(field,"it"). OrderBy("Key descending"). Select("new (Key as Key, Count() as Count)"); // Note: IQueryable doesn't have ToList() implementation - only IEnumerable return output.ToList(); // will not work } 

For List - perhaps you need to add your custom implementation in DynamicLibrary ... See how this was done for the Contains () method here

+2
source

The solution is to use the .QueryByCube LINQ extension method provided by my AdaptiveLINQ product.

Disclaimer: I'm an AdaptiveLINQ developer

0
source

Source: https://habr.com/ru/post/1342138/


All Articles