LINQ查询返回DataTable类型_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > LINQ查询返回DataTable类型

LINQ查询返回DataTable类型

 2014/7/23 11:40:35  Mark1997  程序员俱乐部  我要评论(0)
  • 摘要:个人感觉Linq实用灵活性很大,参考一篇大牛的文章LINQ查询返回DataTable类型http://xuzhihong1987.blog.163.com/blog/static/26731587201101853740294/附上自己写的一个测试程序源代码。//创建自定义DataTableString[]_sFiled=newString[]{"ID","PC","EPC","CRC","RSSI","FREQANT","INVCOUNT"};#regionLinq写法//LinQ写法1
  • 标签:

个人感觉Linq实用灵活性很大,参考一篇大牛的文章LINQ查询返回DataTable类型 

http://xuzhihong1987.blog.163.com/blog/static/26731587201101853740294/

 

附上自己写的一个测试程序源代码

class="code_img_closed" src="/Upload/Images/2014072311/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('139f9dac-a197-436c-85fd-e72610f9fe00',event)" src="/Upload/Images/2014072311/2B1B950FA3DF188F.gif" alt="" />
  //创建自定义DataTable
             String[] _sFiled = new String[] { "ID", "PC", "EPC", "CRC", "RSSI", "FREQANT", "INVCOUNT" };
# region Linq写法
             //  LinQ写法1:
             //var numbers_1 = from number in numbers where (number % 2 == 0) orderby number descending select number;//orderby number descending 这是对筛选出来的数值进行排序
             //   foreach (var i in numbers_1) 
             //   { 
             //   Console.WriteLine(i); 
             //   }
             //LinQ写法2:
             //  var numbers_1 = numbers.Where(i => i % 2 == 0).Select(i => i);//输出用写法1一样
#endregion
            
             DataTable    _dtAudit = CreateSelfDataTable(_sFiled);

             _dtAudit.Rows.Add("a1", "b1", "c1","","","","");
             _dtAudit.Rows.Add("a1", "b2", "c2", "", "", "", "");
             _dtAudit.Rows.Add("a2", "b3", "c3", "", "", "", "");
             _dtAudit.Rows.Add("a3", "b4", "c4", "", "", "", "");
             _dtAudit.Rows.Add("a1", "b5", "c5", "", "", "", "");
             _dtAudit.Rows.Add("a2", "b6", "c6", "", "", "", "");

             var query1 = (from contact in _dtAudit.AsEnumerable()                                      //查询
                           orderby contact.Field<string>("ID") descending                                      //排序
                           group contact by contact.Field<string>("PC") into g                               //分组
                           select new {ID=g.FirstOrDefault().Field<string>("ID"),PC=g.FirstOrDefault().Field<string>("PC"),EPC=
                               g.FirstOrDefault().Field<string>("EPC"),CRC=g.FirstOrDefault().Field<string>("CRC"),RSSI=g.FirstOrDefault(
                               ).Field<string>("RSSI")}).ToList();
            DataTable contacts1 =ToDataTable(query1);
           SetListView(listView1, contacts1);
        }

        /// <summary>
        /// 创建自定义列DataTable
        /// </summary>
        /// <param name="sList"></param>
        /// <returns></returns>
        public    DataTable CreateSelfDataTable(String[] sList)
        {
            DataTable _dtSelf = new DataTable();

            foreach (String s in sList)
            {
                _dtSelf.Columns.Add(s);
            }
            _dtSelf.AcceptChanges();

            return _dtSelf;
        }
          


/// <summary>
 ///  下面通过一个方法来实现返回DataTable类型 
 /// LINQ返回DataTable类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="varlist"></param>
/// <returns></returns>
        public static DataTable ToDataTable<T>(IEnumerable<T> varlist)
        {
            DataTable dtReturn = new DataTable();
            // column names 
            PropertyInfo[] oProps = null;
            if (varlist == null)
                return dtReturn;
            foreach (T rec in varlist)
            {
                if (oProps == null)
                {
                   oProps = ((Type)rec.GetType()).GetProperties();
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType;
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                             == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }
                DataRow dr = dtReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                    (rec, null);
                }
                dtReturn.Rows.Add(dr);
            }
            return dtReturn;
        }

        /// <summary>
        /// 绑定ListView
        /// </summary>
        private void SetListView(System.Windows.Forms.ListView listView, DataTable dt)
        {
            if (listView.Items.Count > 50)
            {
                listView.Items.Clear();
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                ListViewItem lv = null;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lv = new ListViewItem(dt.Rows[i][0].ToString());                           //创建一列的多行对象
                    lv.Tag = dt.Rows[i][0].ToString();                                         //该lv对象绑定多行一列值
                    for (int j = 1; j < dt.Columns.Count; j++)
                    {
                        lv.SubItems.Add(dt.Rows[i][j].ToString());                             //一行添加多列的值
                    }
                    listView.Items.Add(lv);
                }

            }
        }
View Code
  • 相关文章
发表评论
用户名: 匿名