动态Linq GroupBy [英] Dynamic Linq GroupBy

查看:124
本文介绍了动态Linq GroupBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照以下步骤来解决类似的问题,但是我似乎无法获得解决方案的工作
System.LINQ.Dynamic:选择(new(...))到List< T> (或任何其他可枚举的< T>集合)



我甚至创建了一个项目,使其与所示的完全相同。 >

我收到一条错误,表示DataRow中存在没有财产或字段基金



我试过解决这个问题:

  IQueryable< Result> res = table1.AsEnumerable()。AsQueryable()
.GroupBy(it [\Fund \],it)
.Select< Result>(new Group,Sum(Convert.ToDouble(it [\Value\]。ToString()))作为TotalValue));

但是当这让我超越上述错误时,我现在有一个问题,得到总和,因为错误状态


参数类型不匹配


不确定从哪里去
谢谢

解决方案

  IQueryable< Result> res = table1.AsEnumerable()。AsQueryable()
.GroupBy(it [\Fund \],it)
.Select< Result>(new(Key。 ToString()作为Group,Sum(Convert.ToDouble(it [\Value\]。ToString()))作为TotalValue));

这样做! - 需要在密钥上执行 ToString()

I followed the bellow steps to solve a similar problem as described however i dont seem to be able to get the solution to work System.LINQ.Dynamic: Select(" new (...)") into a List<T> (or any other enumerable collection of <T>)

I have even created a project so that it is the exact same as shown.

I get an error saying that "No property or field 'Fund' exists in type 'DataRow'"

I tried to get around this problem with:

IQueryable<Result> res = table1.AsEnumerable().AsQueryable()
                              .GroupBy("it[\"Fund\"]", "it")
                              .Select<Result>("new (Key as Group, Sum(Convert.ToDouble(it[\"Value\"].ToString())) as  TotalValue)");

but while this gets me past the above error i now have a problem getting the sum as the error states

Argument types do not match

Not sure where to go from here Thanks

解决方案

IQueryable<Result> res = table1.AsEnumerable().AsQueryable()
                               .GroupBy("it[\"Fund\"]", "it")
                               .Select<Result>("new (Key.ToString() as Group, Sum(Convert.ToDouble(it[\"Value\"].ToString())) as TotalValue)");

That works!! - needed to do a ToString() on the key

这篇关于动态Linq GroupBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆