从 List复制数据到数据表对象

发布于 2024-11-25 16:36:40 字数 1103 浏览 7 评论 0原文

我有一个列表<>对象(其中对象是自定义实体类),我有一个 DataTable,其中包含与实体类的属性匹配的列。

有没有一种方法可以将列表中的数据项复制到数据表中,而无需循环遍历列表并手动将数据添加到数据表中。

以下是我当前代码 (C# 4.0) 的示例:

    void MergeData()
    {
        List<MyEntity> myEntities = GetEntities();

        // Create a DataTable based on the Properties of the MyEntity class
        Type entity = typeof(MyEntity);
        PropertyInfo[] properties = entity.GetProperties();
        DataTable dt = new DataTable();
        foreach (PropertyInfo pi in properties)
        {
            dt.Columns.Add(pi.Name);
        }

        // Here's where I loop through the List and fill the DataTable. 
        // Is there a way to fill the DataTable without looping through the List?    
        foreach (MyEntity e in myEntities)
        {
            DataRow dr = dt.NewRow();
            foreach (PropertyInfo pi in properties)
            {
                dr[pi.Name] = pi.GetValue(e, null);
            }
            dt.Rows.Add(dr);
        }
    }

通常,List<>将有大约 27k 个项目,所以我只是好奇是否有更干净和/或更优化的方法来从我的 List<> 获取数据到数据表中。

I have a a List<> of objects (where the objects are a custom entity class) and I have a DataTable which has columns that match the Properties of the Entity class.

Is there a way I can copy the data items with the List to the DataTable without having to loop through the List and manually adding the data to the DataTable.

Here's a sample of my current code (C# 4.0):

    void MergeData()
    {
        List<MyEntity> myEntities = GetEntities();

        // Create a DataTable based on the Properties of the MyEntity class
        Type entity = typeof(MyEntity);
        PropertyInfo[] properties = entity.GetProperties();
        DataTable dt = new DataTable();
        foreach (PropertyInfo pi in properties)
        {
            dt.Columns.Add(pi.Name);
        }

        // Here's where I loop through the List and fill the DataTable. 
        // Is there a way to fill the DataTable without looping through the List?    
        foreach (MyEntity e in myEntities)
        {
            DataRow dr = dt.NewRow();
            foreach (PropertyInfo pi in properties)
            {
                dr[pi.Name] = pi.GetValue(e, null);
            }
            dt.Rows.Add(dr);
        }
    }

Typically, the List<> will have around 27k items so I'm simply curious if there is a cleaner and/or more optimized way to get data from my List<> into a DataTable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-12-02 16:36:40

如何使您的实体能够填充可以添加到数据表中的数据行。这样就可以减少反射的使用。

How about making your entities have ability to populate a data row which could be added to your data table. Thus way you reduce the usage of reflection.

只是一片海 2024-12-02 16:36:40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for UserBO
/// </summary>
public class UserBO
{
    public UserBO()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    private string _mUserName;

    public string UserName
    {
        get { return _mUserName; }
        set { _mUserName = value; }
    }
    private string _mPassword;

    public string Password
    {
        get { return _mPassword; }
        set { _mPassword = value; }
    }
    private int _mUserID;

    public int UserID
    {
        get { return _mUserID; }
        set { _mUserID = value; }
    }
}

 IList<UserBO> objlistUserBo = new List<UserBO>();
 objlistUserBo = objUserFacade.Getuserinfo(objUSerBO);

            for (int iterator=0; iterator < objlistUserBo.Count; iterator++)
            {
                objUSerBO = (UserBO)objlistUserBo[iterator];
                string name = objUSerBO.UserName;
                string pwd = objUSerBO.Password;
            }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for UserBO
/// </summary>
public class UserBO
{
    public UserBO()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    private string _mUserName;

    public string UserName
    {
        get { return _mUserName; }
        set { _mUserName = value; }
    }
    private string _mPassword;

    public string Password
    {
        get { return _mPassword; }
        set { _mPassword = value; }
    }
    private int _mUserID;

    public int UserID
    {
        get { return _mUserID; }
        set { _mUserID = value; }
    }
}

 IList<UserBO> objlistUserBo = new List<UserBO>();
 objlistUserBo = objUserFacade.Getuserinfo(objUSerBO);

            for (int iterator=0; iterator < objlistUserBo.Count; iterator++)
            {
                objUSerBO = (UserBO)objlistUserBo[iterator];
                string name = objUSerBO.UserName;
                string pwd = objUSerBO.Password;
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文