English 中文(简体)
Linq to DataTable without enumerating fields
原标题:

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

问题回答

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.

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签