Linq to sql LINQ2SQL到数据表

Linq to sql LINQ2SQL到数据表,linq-to-sql,ado.net,Linq To Sql,Ado.net,将LINQ2SQL表复制到ADO.NET DataTable中最简单的方法是什么?下面是一篇关于扩展方法的博文,我认为它可以满足您的需要: 基思·埃尔德的代码 /// <summary> /// This will take anything that implements the ICollection interface and convert /// it to a DataSet. /// </summary> /// <example>

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

下面是一篇关于扩展方法的博文,我认为它可以满足您的需要:

基思·埃尔德的代码

/// <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;

    }

}
//
///这将获取实现ICollection接口的任何内容并进行转换
///将其转换为数据集。
/// 
/// 
///CollectiontoDataSet转换器=新CollectiontoDataSet(字母);
///DataSet ds=converter.CreateDataSet();
/// 
/// 
公共类CollectionToDataSet,其中T:System.Collections.ICollection
{
T_集合;
公共集合到数据集(T列表)
{
_收集=列表;
}
private PropertyInfo[]\u propertyCollection=null;
私有财产信息[]财产集合
{
得到
{
if(_propertyCollection==null)
{
_propertyCollection=GetPropertyCollection();
}
归还财产集合;
}
}
私有属性信息[]GetPropertyCollection()
{
如果(_collection.Count>0)
{
IEnumerator枚举器=_collection.GetEnumerator();
枚举数。MoveNext();
返回枚举数.Current.GetType().GetProperties();
}
返回null;
}
公共数据集CreateDataSet()
{
数据集ds=新数据集(“GridDataSet”);
Add(FillDataTable());
返回ds;
}
私有数据表FillDataTable()
{
IEnumerator枚举器=_collection.GetEnumerator();
DataTable dt=CreateDataTable();
while(枚举数.MoveNext())
{
Add(FillDataRow(dt.NewRow(),enumerator.Current));
}
返回dt;
}
私有DataRow FillDataRow(DataRow DataRow,对象p)
{
foreach(PropertyInfo property在PropertyCollection中)
{
dataRow[property.Name.ToString()]=property.GetValue(p,null);
}
返回数据行;
}
私有数据表CreateDataTable()
{
DataTable dt=新的DataTable(“GridDataTable”);
foreach(PropertyInfo property在PropertyCollection中)
{
Add(property.Name.ToString());
}
返回dt;
}
}