zoukankan      html  css  js  c++  java
  • linq 查询结果转换成DataTable 方法

    转换方法代码:

    代码
     private DataTable ConvertToTable(IQueryable query)
            {
                DataTable dtList 
    = new DataTable();
                
    bool isAdd = false;
                PropertyInfo[] objProterties 
    = null;
                
    foreach (var item in query)
                {
                    
    if (!isAdd)
                    {
                        objProterties 
    = item.GetType().GetProperties();
                        
    foreach (var itemProterty in objProterties)
                        {
                            Type type 
    = null;
                            
    if (itemProterty.PropertyType != typeof(string&& itemProterty.PropertyType != typeof(int&& itemProterty.PropertyType != typeof(DateTime))
                            {
                                type 
    = typeof(string);
                            }
                            
    else
                            {
                                type 
    = itemProterty.PropertyType;
                            }
                            dtList.Columns.Add(itemProterty.Name, type);
                        }
                        isAdd 
    = true;
                    }
                    var row 
    = dtList.NewRow();
                    
    foreach (var pi in objProterties)
                    {
                        row[pi.Name] 
    = pi.GetValue(item, null);
                    }
                    dtList.Rows.Add(row);
                }

                
    return dtList;
            }


    调用方法:

    //linq查询语句
    var queryData =
                                from dv 
    in emcsDataContext.DailyVals
                                join vrb 
    in emcsDataContext.Variables on dv.VariableID equals vrb.VariableID
                                
    where dv.VariableID == variableId && dv.CollectTime >= selectDate && dv.CollectTime < selectDate.AddDays(1)
                                orderby dv.CollectTime
                                select 
    new
                                {
                                    dv.ID,
                                    dv.VariableID,
                                    dv.CollectTime,
                                    dv.VariableValue,
                                    vrb.UpLimite,
                                    vrb.LowLimite,
                                    statDate 
    = dv.CollectTime.ToShortTimeString()
                                };
    //转换
    ConvertToTable(queryData)


  • 相关阅读:
    面试题
    面试题-基础篇(1)
    Windows定时器学习
    C++中类的多态与虚函数的使用
    Win32 SDK 编程开始, 创建窗口, 消息的处理, 消息循环
    线程同步(互斥锁与信号量的作用与区别)
    控制台程序实现暂停功能
    Linux下gcc编译生成动态链接库*.so文件并调用它
    Linux下安装ActiveMQ CPP
    activemq Linux下的编译
  • 原文地址:https://www.cnblogs.com/scottckt/p/1786007.html
Copyright © 2011-2022 走看看