Linq to DataTable 不枚举字段

发布于 2024-08-12 17:51:31 字数 380 浏览 6 评论 0原文

我正在尝试查询 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 技术交流群。

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

发布评论

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

评论(2

骄傲 2024-08-19 17:51:31

如果我理解正确,您希望获得不需要在代码中定义但可以强类型方式使用的对象集合。可悲的是,不,你不能。

匿名类型看起来像是某种变体动态对象,但它实际上是在编译时定义的强类型类。 .NET 在幕后自动为您定义类型。为了让 .net 能够做到这一点,它必须从代码中获得一些线索来推断类型定义。它必须有类似的东西:

from ItemA in ItemData.AsEnumerable()
select ItemA.Item("Name"), ItemA.Item("Email") 

所以它知道要定义哪些成员。没有办法绕过它,信息必须在逻辑上存在才能定义匿名类型。

根据您尝试执行此操作的具体原因,有一些选择。

  • 如果您希望在封装数据访问的同时进行智能感知,则可以从封装的数据访问类中返回 xml 而不是数据表。 (您可以非常轻松地将数据表转换为 xml。您将需要使用新的 System.Xml.Linq 类,例如 XElement。它们太棒了!)然后您可以使用 VS2008 的功能从 xml 创建 xsd 架构。然后使用/导入代码页顶部的该架构,您就拥有了智能感知。
  • 如果您必须拥有一个带有数据属性的对象,但又不想为它们定义类/结构,那么您会喜欢新的动态对象即将推出 C#4.0/VB10。您拥有基于 sql 返回内容的对象属性,但您不会拥有智能感知。这也会带来性能成本,但是(a)这对于您的情况可能并不重要,并且(b)在某些情况下实际上并没有那么糟糕。
  • 如果您只是想避免创建大量类,请考虑在类定义下方的同一代码文件上定义结构/结构。当您向结果集中添加更多列时,可以轻松调整具有更多公共字段的结构。

简而言之,您可以拥有以下三个中的任何两个:(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:

from ItemA in ItemData.AsEnumerable()
select ItemA.Item("Name"), ItemA.Item("Email") 

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.

  • If you want intellisense while still encapsulating your data access, you can return xml instead of a datatable from your encapsulated data access class. (You can convert data tables to xml very easily. You'll want to use the new System.Xml.Linq classes like the XElement. They're great!) Then you can use VS2008's ability to create an xsd schema from xml. Then use/import that schema at the top of your code page, and you have intellisense.
  • If you have to have an object an with properties for your data, but don't want to define a class/structure for them, you'll love the new dynamic objects coming in C#4.0/VB10. You have object properties based on what the sql returns, but you won't have intellisense. There is also a performance cost to this, but (a) that might not matter for your situation and (b) it actually is not so bad in some situations.
  • If you're just trying to avoid making a lot of classes, consider defining structs/structures on the same code file, beneath your class definition. When you add more columns to your result set, it's easy to adjust a struct with more public fields.

In short you can have any two of the following three: (a) dynamic, (b) strontly-typed objects, (3) intellisense. But not all three.

心不设防 2024-08-19 17:51:31

有一种方法可以完成您想要的任务,但它需要动态 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

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