当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to get DataTable records in to list using lambda expression?

问题描述:

This question already has an answer here:

  • How do you convert a DataTable into a generic list?

    18 answers

网友答案:

You could use either .Field linq extension or index/columnname (ex row["columnname"] to access values from a DataRow. I suggest using Field extension as it even handle nullable types.

data  = dt.AsEnumerable()
  .Select(row=>  new EmployeeProp ()
   {
       Name = row.Field<string>("Name"),
       Email = row.Field<string>("Email ") 
       // Other properties....      
   })
   .ToList();
网友答案:

you can try this too, make EmployeeProp() object

data = (from a in dt
                select new EmployeeProp() { Name = a.Name, Email = a.Email }).ToList();
网友答案:
 var myData = (from a in dt.AsEnumerable()
               select new EmployeeProp { 
                   Name = a.Field<string>("Name"), 
                   Email = a.Field<string>("Email") 
              }).ToList();
分享给朋友:
您可能感兴趣的文章:
随机阅读: