Saturday, 15 May 2010

c# - Linq to DataTable without enumerating fields -


I am trying to query a DataTable object without specifying the field like:

 Var linqdata = Select itemData.AsEnumerable () from ItemA ItemA  

but the type of return is

  System.Data.EnumerableRowCollection & Lt; System.Data. DataRow & gt;  

and I need the following return type

  System.Data.EnumerableRowCollection & lt; & Lt; Object, object & gt; & Gt;  

(like standard anonymous type)

Any ideas? Thank you

If I understand you correctly, you want to get a collection of objects, t Need to define in your code, but it is usable to use it in a strongly typed fashion. Unfortunately, no, you can not.

A anonymous type looks like any type of type or dynamic object , but it is actually a strongly typed Is a class, which is defined in time compilation. .NET automatically defines the type behind the scenes for you. For .net to be able to do this, there should be some clue with that code with which the definition of type can be estimated. It will have something like this: ItemA.Item ("name"), ItemA.Item ("email") from ItemA in ItemAdata.AsEnumerable ()

So it knows what the members have to define. There is no way to get around it, the information must be for an anonymous type to be rationally defined.

Depending on why you are trying to do this, there are some options.

  • If you still want intellisense while increasing your data access, you can return XML instead of a datatable to your encoded data access class. (You can easily convert the data table to XML. You want to like the new System.Xml.Linq sections. They are very good!) Then you can get the VS2 Xsd schema xml again Use / Import the schema at the top of your code page and you have intellisense.
  • If you have a object with properties for your data, but do not want to define a class / structure for them, you should come in C # 4.0 / VB 10 Will love the new dynamic objects . You have an object property based on SQL, but you will not be intelligent. There is also a display cost for this, but (A) that does not matter to your situation and (b) this is not really so bad in some situations.
  • If you are just trying to avoid too many classes, consider defining Stront / structures on the same code file below your class definition. When you add more columns to your result set, adjusting a structure with more public areas is easy.

In short, you can have two in the following three: (A) dynamic, (b) strontally-typed object, (3) intelligence, but not all three.


No comments:

Post a Comment