LINQ DynamicLibrary:如何从 IQueryable 中提取计数和列表

发布于 10-20 10:11 字数 1419 浏览 5 评论 0原文

我已经开始尝试使用 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();
}

我无法从分组中提取 CountList 。我该怎么做?我花了相当多的时间寻找解决方案。如果可能的话,我希望能够避免使用反射。我在以下链接的前面有一些指示,但这对我的实际问题没有帮助。预先感谢您的任何帮助。

System.LINQ.Dynamic: 选择( “new (...)”) 到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨巷深深2024-10-27 10:11:08

对于Count,可以编写如下:

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
}

对于List - 可能您需要将自定义实现添加到DynamicLibrary...
了解 Contains() 方法是如何完成的 此处

For Count it can be written as following:

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 - possible you'll need to add your custom implementation to DynamicLibrary...
See how it was done for Contains() method here

冬天旳寂寞2024-10-27 10:11:08

解决方案是使用我的产品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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文