Linq to DataTable 不枚举字段
我正在尝试查询 DataTable 对象而不指定字段,如下所示:
var linqdata = from ItemA in ItemData.AsEnumerable()
select ItemA
但返回类型是
System.Data.EnumerableRowCollection<System.Data.DataRow>
,我需要以下返回类型
System.Data.EnumerableRowCollection<<object,object>>
(如标准匿名类型)
任何想法? 谢谢
i´m trying to query a DataTable object without specifying the fields, like this :
var linqdata = from ItemA in ItemData.AsEnumerable()
select ItemA
but the returning type is
System.Data.EnumerableRowCollection<System.Data.DataRow>
and I need the following returning type
System.Data.EnumerableRowCollection<<object,object>>
(like the standard anonymous type)
Any idea?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您希望获得不需要在代码中定义但可以强类型方式使用的对象集合。可悲的是,不,你不能。
匿名类型看起来像是某种变体或动态对象,但它实际上是在编译时定义的强类型类。 .NET 在幕后自动为您定义类型。为了让 .net 能够做到这一点,它必须从代码中获得一些线索来推断类型定义。它必须有类似的东西:
所以它知道要定义哪些成员。没有办法绕过它,信息必须在逻辑上存在才能定义匿名类型。
根据您尝试执行此操作的具体原因,有一些选择。
简而言之,您可以拥有以下三个中的任何两个:(a) 动态、(b) 强类型对象、(3) 智能感知。但并非全部三个。
If I understand you correctly, you'd like to get a collection of objects that you don't need to define in your code but that are usable in a strongly typed fashion. Sadly, no you can't.
An anonymous type seems like some kind of variant or dynamic object, but it is in fact a strongly typed class that is defined at compile time. .NET defines the type for you automatically behind the scenes. In order for .net to be able to do this, it has to have some clue from the code with which to infer the type definition. It has to have something like:
so it knows what members to define. There's no way to get around it, the information has to logically be there for the anonymous type to be defined.
Depending on why exactly your are trying to do this, there are some options.
In short you can have any two of the following three: (a) dynamic, (b) strontly-typed objects, (3) intellisense. But not all three.
有一种方法可以完成您想要的任务,但它需要动态 linq 的知识。您可以在运行时构建查询,然后使用它。我不是专家,也从未真正使用过它,但这里有一个关于 Scott Guthrie 的博客的链接 - 动态 Linq。希望有帮助。
韦德
There is one way to accomplish what you want, but it required knowledge of dynamic linq. You would build the query during run-time and then use it. I am no expert and have never really played around with it, but here is a link to Scott Guthrie's blog about it - Dynamic Linq. Hope that helps.
Wade