LINQ2SQL 到数据表

发布于 2024-08-20 05:46:23 字数 51 浏览 1 评论 0原文

将 LINQ2SQL 表复制到 ADO.NET DataTable 的最简单方法是什么?

What is the easiest way of copying a LINQ2SQL table into an ADO.NET DataTable?

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

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

发布评论

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

评论(1

∝单色的世界 2024-08-27 05:46:23

这是一篇关于扩展方法的博客文章,我认为它可以满足您的需求:

http://iandykes.blogspot.com/2008/05/ienumerable-to-dataset-extension-method.html

代码由 Keith Elder 编写

/// <summary>

/// This will take anything that implements the ICollection interface and convert

/// it to a DataSet.

/// </summary>

/// <example>

/// CollectiontoDataSet converter = new CollectionToDataSet<Letters[]>(letters);

/// DataSet ds = converter.CreateDataSet();

/// </example>

/// <typeparam name="T"></typeparam>

public class CollectionToDataSet<T> where T : System.Collections.ICollection

{

    T _collection;

    public CollectionToDataSet(T list)

    {

        _collection = list;

    }



    private PropertyInfo[] _propertyCollection = null;

    private PropertyInfo[] PropertyCollection

    {

        get

        {

            if (_propertyCollection == null)

            {

                _propertyCollection = GetPropertyCollection();

            }

            return _propertyCollection;

        }

    }



    private PropertyInfo[] GetPropertyCollection()

    {

        if (_collection.Count > 0)

        {

            IEnumerator enumerator = _collection.GetEnumerator();

            enumerator.MoveNext();

            return enumerator.Current.GetType().GetProperties();

        }

        return null;

    }



    public DataSet CreateDataSet()

    {

        DataSet ds = new DataSet("GridDataSet");

        ds.Tables.Add(FillDataTable());

        return ds;

    }



    private DataTable FillDataTable()

    {

        IEnumerator enumerator = _collection.GetEnumerator();

        DataTable dt = CreateDataTable();

        while (enumerator.MoveNext())

        {

            dt.Rows.Add(FillDataRow(dt.NewRow(),enumerator.Current));

        }

        return dt;

    }



    private DataRow FillDataRow(DataRow dataRow, object p)

    {

        foreach (PropertyInfo property in PropertyCollection)

        {

            dataRow[property.Name.ToString()] = property.GetValue(p, null);

        }

        return dataRow;

    }



    private DataTable CreateDataTable()

    {

        DataTable dt = new DataTable("GridDataTable");

        foreach (PropertyInfo property in PropertyCollection)

        {

            dt.Columns.Add(property.Name.ToString());

        }

        return dt;

    }

}

Here is a blog post on an extension method that i think would do what you want:

http://iandykes.blogspot.com/2008/05/ienumerable-to-dataset-extension-method.html

code by Keith Elder

/// <summary>

/// This will take anything that implements the ICollection interface and convert

/// it to a DataSet.

/// </summary>

/// <example>

/// CollectiontoDataSet converter = new CollectionToDataSet<Letters[]>(letters);

/// DataSet ds = converter.CreateDataSet();

/// </example>

/// <typeparam name="T"></typeparam>

public class CollectionToDataSet<T> where T : System.Collections.ICollection

{

    T _collection;

    public CollectionToDataSet(T list)

    {

        _collection = list;

    }



    private PropertyInfo[] _propertyCollection = null;

    private PropertyInfo[] PropertyCollection

    {

        get

        {

            if (_propertyCollection == null)

            {

                _propertyCollection = GetPropertyCollection();

            }

            return _propertyCollection;

        }

    }



    private PropertyInfo[] GetPropertyCollection()

    {

        if (_collection.Count > 0)

        {

            IEnumerator enumerator = _collection.GetEnumerator();

            enumerator.MoveNext();

            return enumerator.Current.GetType().GetProperties();

        }

        return null;

    }



    public DataSet CreateDataSet()

    {

        DataSet ds = new DataSet("GridDataSet");

        ds.Tables.Add(FillDataTable());

        return ds;

    }



    private DataTable FillDataTable()

    {

        IEnumerator enumerator = _collection.GetEnumerator();

        DataTable dt = CreateDataTable();

        while (enumerator.MoveNext())

        {

            dt.Rows.Add(FillDataRow(dt.NewRow(),enumerator.Current));

        }

        return dt;

    }



    private DataRow FillDataRow(DataRow dataRow, object p)

    {

        foreach (PropertyInfo property in PropertyCollection)

        {

            dataRow[property.Name.ToString()] = property.GetValue(p, null);

        }

        return dataRow;

    }



    private DataTable CreateDataTable()

    {

        DataTable dt = new DataTable("GridDataTable");

        foreach (PropertyInfo property in PropertyCollection)

        {

            dt.Columns.Add(property.Name.ToString());

        }

        return dt;

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文