dataset to Listusing linq

前端 未结 6 646
别跟我提以往
别跟我提以往 2021-02-06 13:18

I have a DataSet and I want to convert the DataSet into List

T - type object

How convert my DataSet?

相关标签:
6条回答
  • 2021-02-06 13:51

    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);
                        }
    
    0 讨论(0)
  • 2021-02-06 13:52

    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

    0 讨论(0)
  • 2021-02-06 13:56

    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();
    
    0 讨论(0)
  • 2021-02-06 14:05

    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.

    0 讨论(0)
  • 2021-02-06 14:08

    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.

    0 讨论(0)
  • 2021-02-06 14:12

    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
    
    0 讨论(0)
提交回复
热议问题