简体   繁体   English

.net如何为IEnumerable设置匿名类型

[英].net How can set the anonymous type for IEnumerable

I need to Convert a Ienumerable<T> to a dataset. 我需要将Ienumerable<T>转换为数据集。 For this i need to write a common fnction to convert any IEnumerable type to Dataset. 为此,我需要编写一个公共函数以将任何IEnumerable类型转换为Dataset。 For this I need to set anonymous type. 为此,我需要设置匿名类型。 Here is the Code 这是代码

public static DataTable ToDataTable(Object alist)
    {
        String AssemblyName  = "DataAccessLayer";
        String ClassName = "sptblSystemPreference_GetListResult";
        Type ObjType = Type.GetType(ClassName + "," + AssemblyName);

//Below line working fine... But how can I implement ObjType in the IEnumerable
        IEnumerable<DataAccessLayer.sptblSystemPreference_GetListResult> objList1 = (IEnumerable<DataAccessLayer.sptblSystemPreference_GetListResult>)alist;
        List<DataAccessLayer.sptblSystemPreference_GetListResult> objList = objList1.ToList();

        DataTable dt = new DataTable();

        if (objList[0] != null)
        {
            dt.TableName = objList[0].GetType().Name;
            System.Reflection.PropertyInfo[] propInfo = objList[0].GetType().GetProperties();
            for (int PropertyCount = 0; PropertyCount < propInfo.Length; PropertyCount++)
            {
                dt.Columns.Add(propInfo[PropertyCount].Name);
            }
            for (int row = 0; row < objList.Count; row++)
            {
                DataRow dr;
                dr = dt.NewRow();
                for (int PropertyCount = 0; PropertyCount < propInfo.Length; PropertyCount++)
                {

                    Object obj = propInfo[PropertyCount].GetValue(objList[row], null);
                    if(obj!=null)
                        dr[PropertyCount] =  obj;
                }
                dt.Rows.Add(dr);
            }
        }
        return dt;
    }
}

I implemented this to convert a IList<T> to a DataTable and documented it on my blog: 我实现了将IList<T>转换为DataTable并将其记录在我的博客中:

public static class ListExtensions
{
    public static DataTable ToDataTable<T>(this IList<T> list)
    {
        IList<PropertyInfo> properties = list.GetPropertiesOfObjectInList();
        DataTable resultTable = CreateTable(properties);

        foreach(var item in list)
        {
            var row = CreateRowFromItem<T>(resultTable, item);
            resultTable.Rows.Add(row);
        }

        return resultTable;
    }

    private static DataTable CreateTable(IList<PropertyInfo> properties)
    {
        DataTable resultTable = new DataTable();
        foreach (var property in properties)
        {
            resultTable.Columns.Add(property.Name, property.PropertyType);
        }
        return resultTable;
    }

    public static IList<PropertyInfo> GetPropertiesOfObjectInList<T>(this IList<T> list)
    {
        return typeof(T).GetProperties().ToList();
    }

    private static DataRow CreateRowFromItem<T>(DataTable resultTable, T item)
    {
        var row = resultTable.NewRow();
        var properties = item.GetType().GetProperties().ToList();
        foreach (var property in properties)
        {
            row[property.Name] = property.GetValue(item, null);
        }
        return row;
    }
}

This allows you to write code like DataTable yourTable = yourList.ToDataTable() . 这使您可以编写类似DataTable yourTable = yourList.ToDataTable()代码。 You can read about it here: Generic list to DataTable 您可以在此处阅读: DataTable的通用列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM