Kasrak
Kasrak

Reputation: 1561

Select and Cast to convert a Generic List into DataRow[]

There is a typed DataSet Named DsPerson And a custom Typed Generic List Named ListPerson

DsPerson Ds1Person;

List<Person> ListPerson;

Want to do something similar to the code below,

please help me correcting this :

 DataTable dtPerson = Ds1Person.Tables["Person"];
 DataRow drPerson = dtPerson.NewRow();

 DsPerson.PersonRow[] updRows = ListPerson.Cast<Person>().Select(row => dtPerson.NewRow()
                      {
                          Nick = row.Nick,
                          Name = row.NameX,
                       }).ToArray();

Needed a DataRow Array or a general DataTable made this way.

Not looking for Entities here or any other alternatives because of the code specifications I'm working on.

Any help is really appreciated ...


More Clarifying :

What Is needed :

Something Like reversing these : (Opposite way)

http://www.stackoverflow.com/questions/441023/fastest-way-to-convert-datatable-to-generic-list

or a cleaner and Linqy way of doing something similar to this :

http://beecy.net/post/2009/05/18/convert-generic-list-to-datatable-using-reflection.aspx

Get the proper DsPerson.PersonRow[] updRows to use with adapter.Update(updRows);

What Is wrong ?

The Cast - Select Statement have slight errors : I just knew it should be similar to this, so I'm not sure on the structure of this line of code,

for now the reported error is :

Error 2 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'DsPerson.PersonRow[]'. An explicit conversion exists (are you missing a cast?)

Upvotes: 0

Views: 5376

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460340

The same should work for a strong typed DataSet:

DataRow[] updRows = 
    ListPerson.Select(p => dtPerson.Rows.Add(p.Nick, p.Name)).ToArray();

Upvotes: 2

Related Questions