Linq to DataTable而不枚举字段 [英] Linq to DataTable without enumerating fields

查看:96
本文介绍了Linq to DataTable而不枚举字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不指定字段的情况下查询DataTable对象,如下所示:

i´m trying to query a DataTable object without specifying the fields, like this :

var linqdata = from ItemA in ItemData.AsEnumerable()
select ItemA

,但返回类型为

System.Data.EnumerableRowCollection<System.Data.DataRow>

我需要以下返回类型

System.Data.EnumerableRowCollection<<object,object>>

(类似于标准的匿名类型)

(like the standard anonymous type)

有什么主意吗? 谢谢

推荐答案

如果我对您的理解是正确的,那么您将获得不需要在代码中定义但可以在代码中使用的对象的集合.强类型的时尚.可悲的是,不,你不能.

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.

匿名类型似乎是某种 variant 动态对象,但实际上它是在以下位置定义的强类型类编译时间. .NET在幕后自动为您定义类型.为了使.net能够做到这一点,它必须从代码中获得一些线索来推断类型定义.它必须具有以下内容:

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.

  • 如果希望在智能化的同时仍封装数据访问,则可以从封装的数据访问类中返回xml而不是数据表. (您可以非常轻松地将数据表转换为xml.您需要使用新的 System.Xml.Linq 类,例如
  • 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.

简而言之,您可以拥有以下三个中的任意一个两个:(a)动态的;(b)坚固型的对象;(3)智慧.但不是全部三个.

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

这篇关于Linq to DataTable而不枚举字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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