在动态Linq中访问数据 [英] accessing data in dynamic Linq

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

问题描述



我正在使用动态LINQ处理多个groupby的复杂函数。

i有一个查询:

Hi,
I am working on a complex function of multiple groupby using LINQ dynamically.
i have a query like :

var groupByData = dataCollection.AsQueryable().GroupBy(strGroupby.ToString(),"it")
.select(strdata.ToString());



i希望通过我作为groupByData的集合访问数据,但我无法这样做。

当我在快速发现时看看我找到的所有值都是正确的值,通过快速表我发现了一些东西...



system.linq.systemcore_enumerabledebugview< dynamicclass2>( groupByData).items [0] .....但是当我在我的代码中尝试这个时它给了我一个错误。

如何访问我在集合中的数据,它是IQueryable return输入。



非常感谢adv ance。



Nishant


i want to access data through the collection that i have as "groupByData", but i am unable to do so.
When i found out in the quick watch all the values that i have found are the required correct values, going through quickwatch i found out something as...

system.linq.systemcore_enumerabledebugview<dynamicclass2>(groupByData).items[0]..... but it gives me an error when i tried this in my code.
How can i access the data i have in collection , it is of IQueryable return Type.

Thanks a lot in advance.

Nishant

推荐答案

你似乎不理解委托和lambda表达式。

基于 Enumerable 扩展方法的Linq表达式为 GroupBy 等等,需要代表,通常在表单中lambda表达式。



例如函数Select的定义如 Enumerable.Select(... ) [ ^ ]。它需要一个代理人获取一个源项并返回结果项。



例如
You seem to not understand delegates and lambda expressions.
A Linq expression based on the Enumerable extension methods as GroupBy etc. take delegates, usually in the form of lambda expressions.

E.g. the function Select is defined as described in Enumerable.Select(...)[^]. It takes a delegate that takes a source item and returns the result item.

E.g.
static int Square(int i) { return i * i; }

static void Main(string[] args)
{
    var list = new List<int>() { 1, 2, 3 };
    Console.WriteLine(string.Join(", ", list.Select(Square))); // delegate as function reference
    Console.WriteLine(string.Join(", ", list.Select(i=>i*i))); // delegate as lambda expression
}





我不知道你的数据是什么,但我想你想表达的是:



I don''t know what your data are, but I guess you want to express something like:

var groupByData = dataCollection.GroupBy(e=>e.it);



的ToString()。不知道你为什么要转换成字符串...



干杯

Andi


No ToString(). No clue why you want to convert to string...

Cheers
Andi


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

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