Zac Harlan
Zac Harlan

Reputation: 387

Is there a way to populate an strongly typed object when the structure isn't known?

i'd like to be able to query any number of databases with different table layouts, return a datatable then use that datatable to build a strongly typed object. Is there anything out there that does this or comes close without having to code for each different table layout?

Thank you in advance!

Zac

Upvotes: 1

Views: 156

Answers (5)

andrecarlucci
andrecarlucci

Reputation: 6306

You could try subsonic.

It does exactly that, using code generation via T4 templates.

Get more information at http://subsonicproject.com/

Cheers,

André

Upvotes: 1

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

May be you can use LINQ and return Anonymous Types http://msdn.microsoft.com/en-us/library/bb397696.aspx

eg:

var result = (from itm in list where itm.StateID==2 select new {Name = itm.Name, State=Itm.StateID});

Upvotes: 1

this. __curious_geek
this. __curious_geek

Reputation: 43217

No. This is not possible.

In case of typed-datasets, If you need typed-safety at run-time, the type needs to be defined at design-time or you are going to use plain datasets/datatables for your database operations.

Upvotes: 0

Brett Veenstra
Brett Veenstra

Reputation: 48524

Assuming you're trying to get Intellisense benefits - go the code generation route against your tables.

Upvotes: 0

Joel Etherton
Joel Etherton

Reputation: 37543

I'm not sure this is exactly what you're looking for, but the datatable base type has a method called GetTypedTableSchema() that returns an XmlSchemaSet object. You may be able to use this as a roadmap to translate a typed datatable into a strongly typed object.

Upvotes: 0

Related Questions