将数据集转换为列表 [英] Convert DataSet to List

查看:35
本文介绍了将数据集转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 C# 代码

Employee objEmp = new Employee();
List<Employee> empList = new List<Employee>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
    empList.Add(new Employee { Name = Convert.ToString(dr["Name"]), Age = Convert.ToInt32(dr["Age"]) });
}

它使用循环从数据集创建列表.是否有任何直接方法或更短的方法或一行代码将数据集转换为列表

It uses a loop to create a List from a dataset.Is there any direct method or shorter method or one line code to convert dataset to list

推荐答案

试试这样的:

var empList = ds.Tables[0].AsEnumerable()
    .Select(dataRow => new Employee
    {
        Name = dataRow.Field<string>("Name")
    }).ToList();

这篇关于将数据集转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆