dataset to Listusing linq

2019-03-16 15:36发布

问题:

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?

回答1:

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


回答2:

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();


回答3:

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.



回答4:

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.



回答5:

Thanks for all the above posts...

I have done it with using Linq Query, for detail visit the link

http://codenicely.blogspot.com/2012/02/converting-your-datatable-into-list.html



回答6:

A very simple approach that I use is following:

List<Obj> objList = new List<Obj>();      
foreach (DataRow _dataRow in dataSet.Tables[0].Rows)
                    {
                        Obj obj = new Obj();
                        obj.Col1 = Convert.ToInt32(_dataRow["Col1"]);
                        obj.Col2 = Convert.ToInt32(_dataRow["Col2"]);
                        obj.Col3 = Convert.ToString(_dataRow["Col3"]);
                        objList.Add(obj);
                    }


标签: linq dataset