Linq to DataTable without enumerating fields?

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.

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.

1 Anonymous type does not mean 'no type' or 'dynamic', it just means that the type is not explicitly stated in the code. – Kirk Broadhurst Apr 13 '10 at 3:36 @Kirk Thank you! I thought I gave a good thorough answer here, but no one seemed to notice until you.

:) – Patrick Karcher Apr 13 '10 at 9:57.

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.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions