首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求怎么将LINQ语句查询出的数据类型转换为DATATABLE类型

2013-09-06 
求如何将LINQ语句查询出的数据类型转换为DATATABLE类型本帖最后由 baonan0811 于 2011-05-31 12:55:59 编

求如何将LINQ语句查询出的数据类型转换为DATATABLE类型
本帖最后由 baonan0811 于 2011-05-31 12:55:59 编辑


private DataTable MergeDataTable(DataTable dt1, DataTable dt2)
        {
            DataTable returnDt = new DataTable();            

            var merge = from main in dt1.AsEnumerable()
                        join station in dt2.AsEnumerable() on main.Field<string>("区站号") equals station.Field<string>("区站号")
                                              select new
                        {
                            区站号 = main.Field<string>("区站号"),
                            日期时间 = main.Field<string>("日期时间"),
                            站名 = station.Field<string>("站名"),
                            市 = station.Field<string>("市"),
                            县 = station.Field<string>("县"),
                            经度 = station.Field<object>("经度"),
                            纬度 = station.Field<object>("纬度"),
                            三小时雨量 = main.Field<object>("三小时累积雨量"),


                        };
            returnDt = merge.ConvertToDataTable();
            return returnDt;
        }    



求ConvertToDataTable()方法
[解决办法]
这种两个表合并以后已经变成IEnumerable<T> (匿名对象)

用反射转吧:

//使用:
merge.ToList().ConvertToDataTable();


using System;
using System.Reflection;
using System.Data;

/// <summary>
/// Convert Generic List Type to DataTable.
/// </summary>
public class List2DataTable
{
    #region "Convert Generic List to DataTable"
    /// <summary>
    /// Convert a List{T} to a DataTable.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="items"></param>
    /// <returns></returns>
    public static DataTable ConvertToDataTable<T>(this List<T> items)
    {
        var tb = new DataTable(typeof(T).Name);

        PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public 
[解决办法]
 BindingFlags.Instance);

        foreach (PropertyInfo prop in props)
        {
            Type t = GetCoreType(prop.PropertyType);
            tb.Columns.Add(prop.Name, t);


        }

        foreach (T item in items)
        {
            var values = new object[props.Length];

            for (int i = 0; i < props.Length; i++)
            {
                values[i] = props[i].GetValue(item, null);
            }
            tb.Rows.Add(values);
        }
        return tb;
    }

    /// <summary>
    /// Determine of specified type is nullable
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    public static bool IsNullable(Type t)
    {
        return !t.IsValueType 
[解决办法]
 (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
    }

    /// <summary>
    /// Return underlying type if type is Nullable otherwise return the type.
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    public static Type GetCoreType(Type t)
    {
        if (t != null && IsNullable(t))
        {
            if (!t.IsValueType)
            {
                return t;
            }
            else


            {
                return Nullable.GetUnderlyingType(t);
            }
        }
        else
        {
            return t;
        }
    }
    #endregion
}

热点排行