tags:

views:

186

answers:

4

I have a DataSet and I want to convert the DataSet into List<T>

T - type object

How convert my DataSet? It has 10 columns, with all 10 properties my object has and it's returning over 15000 rows. I want to return that dataset into List<obj> and loop it how do I do that?

+3  A: 

I think this should do it.

var output = yourDataSet.Tables[0].Rows.Cast<DataRow>().Select(r => new
{
    Column1 = r["Column1"].ToString(),
    Column2 = r["Column2"].ToString(),
    Column3 = r["Column3"].ToString(),
    Column4 = r["Column4"].ToString(),
    Column5 = r["Column5"].ToString(),
    Column6 = r["Column6"].ToString(),
    Column7 = r["Column7"].ToString(),
    Column8 = r["Column8"].ToString(),
    Column9 = r["Column9"].ToString(),
    Column10 = r["Column10"].ToString()
}).ToList();
Kelsey
+1 Please excuse my ignorance, but is this LINQ? (I haven't learned LINQ yet)
JohnB
Yes it is. It's pretty much iterating through all the rows in Table[0] and then creating a List of a new anonymous type that has properties for all the columns. I haven't tested it though so there could be some glitches :)
Kelsey
It's sort of linq - making direct use of the underlying features (lambda expressions + extension methods + closures + anonymous types) that make the linq query comprehension syntax possible.
Joel Coehoorn
+2  A: 

First of all, you're on the right track, but you should be thinking in terms of IEnumerable<T> rather than List<T>. And here is how you would do that:

 var myData = ds.Tables[0].AsEnumerable()
                  .Select(r => new {column1 = r[0].ToString(), 
                                    column2 = r[1].ToString() 
                                    /*etc*/
                          });

Never convert an IEnumerable to a List before you absolutely need to.

Joel Coehoorn
I haven't tested it but wouldn't that make a IEnumberable<DataTable>? Tables[0] returns a DataTable where Tables[0].Rows returns `DataRowCollection`.
Kelsey
@Kelsey - There is a special extension method `AsEnumerable` on DataTable that returns `IEnumerable<DataRow>`.
Greg
@Greg oooooh didnt' know that... intellisense to test my code would have helped :)
Kelsey
@Joel Coehoorn why *never* convert an `IEnumerable` to a `List` before we need to?
JohnB
@JohnB because that forces you to actually enumerate the collection, and often also allocate a lot of new memory. It's very bad for performance.
Joel Coehoorn
+3  A: 

This is pretty much the same as the other answers, but introduces strongly-typed columns.

var myData = ds.Tables[0].AsEnumerable().Select(r => new {
    column1 = r.Field<string>("column1"),
    column2 = r.Field<int>("column2"), 
    column3 = r.Field<decimal?>("column3")
});
var list = myData.ToList(); // For if you really need a List and not IEnumerable
Greg
A: 

I know @bharat asked for a solution using LINQ, but mainly for myself I wanted to compare @Kelsey's solution to the old fashioned way of doing this:

List<Obj> list = new List<Obj>();

foreach (DataRow r in yourDataSet.Tables[0].Rows)
{
    Obj obj = new Obj();
    obj.Column1 = r["Column1"];
    obj.Column2 = r["Column2"];
    obj.Column3 = r["Column3"];
    obj.Column4 = r["Column4"];
    obj.Column5 = r["Column5"];
    obj.Column6 = r["Column6"];
    obj.Column7 = r["Column7"];
    obj.Column8 = r["Column8"];
    obj.Column9 = r["Column9"];
    obj.Column10 = r["Column10"];

    list.Add(obj);
}

Or via constructor:

List<Obj> list = new List<Obj>();

foreach (DataRow r in yourDataSet.Tables[0].Rows)
{
    Obj obj = new Obj(r["Column1"], r["Column2"], r["Column3"], r["Column4"], r["Column5"],r["Column6"], r["Column7"], r["Column8"], r["Column9"],r["Column10"]);

    list.Add(obj);
}

I intentionally left off .ToString() because I think using it depends on the situation.

JohnB