LINQ DynamicLibrary:如何从 IQueryable 中提取计数和列表
我已经开始尝试使用 LINQ DynamicLibrary。我正在尝试用几个或仅一个动态 LINQ 查询替换一堆 LINQ 语句。我现有的静态查询如下:
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();
}
如您所见,我的工作单元是 ServiceItem
。 这一切正常,并为我提供了按区域分组的结果集。使用 DynamicLibrary 我的尝试是能够按任何有效的动态字段进行分组(我将单独处理任何验证)。因此,我尝试使用 DynamicLibrary 编写相同的查询,但不是很成功。这是当然无法编译的新方法:
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();
}
我无法从分组中提取 Count
和 List
。我该怎么做?我花了相当多的时间寻找解决方案。如果可能的话,我希望能够避免使用反射。我在以下链接的前面有一些指示,但这对我的实际问题没有帮助。预先感谢您的任何帮助。
I have started experimenting a bit with the LINQ DynamicLibrary. I am trying to replace a bunch of LINQ statements with a few 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 see, my unit of work is ServiceItem
.
This works all fine and gets me a result set grouped by Region
. Using DynamicLibrary my attempt is to be able to group by any valid dynamic field (I will handle any validations separately). So, I tried writing the same query using DynamicLibrary, but am not very successful. Here is the new method which of course doesn't 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 have been unable to extract neither the Count
nor the List
from the grouping. How do I do this? I have spent considerable amount of time looking for a solution. If possible I want to be able to avoid using Reflection. I have had some pointers in this front at the following link, but it doesn't help my actual problem. Thanks in advance for any help.
System.LINQ.Dynamic: Select(" new (...)") into a List<T> (or any other enumerable collection of <T>)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于Count,可以编写如下:
对于List - 可能您需要将自定义实现添加到DynamicLibrary...
了解 Contains() 方法是如何完成的 此处
For Count it can be written as following:
For List - possible you'll need to add your custom implementation to DynamicLibrary...
See how it was done for Contains() method here
解决方案是使用我的产品AdaptiveLINQ提供的
.QueryByCube
LINQ扩展方法。免责声明:我是 AdaptiveLINQ 开发人员
A solution is to use the
.QueryByCube
LINQ extension method provided by my product AdaptiveLINQ.Disclaimer: I'm the AdaptiveLINQ developer