zoukankan      html  css  js  c++  java
  • LINQ使用CopyToDataTable

    1. DataTable可以也可以使用Linq进行条件的赛选

    注意:当查询结果或者查询数据源无数据则抛异常:数据源中没有 DataRow。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.Data;
    
    namespace PredicateTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("userid", typeof(int));
                dt.Columns.Add("username", typeof(string));
                dt.Columns.Add("age", typeof(int));
                dt.Rows.Add(1, "test1", 12);
                dt.Rows.Add(2, "test2", 24);
                dt.Rows.Add(3, "test3", 15);
                dt.Rows.Add(4, "test4", 20);
    
                //当查询结果或者查询数据源无数据则抛异常:数据源中没有 DataRow。
                //DataTable result1 = (from p in dt.AsEnumerable()
                //                    where p.Field<int>("age") > 1000
                //                    select p).CopyToDataTable<DataRow>();
                //DataTable result2 = dt.AsEnumerable().Where(p => p.Field<int>("age") > 15).CopyToDataTable();
    
                //DataTable result3 = (from p in dt.Rows.Cast<DataRow>()
                //                    where p.Field<int>("age") > 15
                //                    select p).CopyToDataTable();
                //DataTable result4 = dt.Rows.Cast<DataRow>().Where(p => p.Field<int>("age") > 15).CopyToDataTable();
    
                //改进,在使用CopyToDataTable方法之前先判断一下是否有数据
                var temp1 = from p in dt.AsEnumerable()
                            where p.Field<int>("age") > 15
                            select p;
                var temp2 = dt.AsEnumerable().Where(p => p.Field<int>("age") > 15);
                if (temp1.Count() > 0)
                {
                    DataTable result1 = temp1.CopyToDataTable();
                }
                if (temp2.Count() > 0)
                {
                    DataTable result1 = temp1.CopyToDataTable();
                }
    
                var temp3 = from p in dt.Rows.Cast<DataRow>()
                            where p.Field<int>("age") > 15
                            select p;
                if (temp3.Count() > 0)
                {
                    DataTable result3 = temp3.CopyToDataTable();
                }
                var temp4 = dt.Rows.Cast<DataRow>().Where(p => p.Field<int>("age") > 15);
                if (temp4.Count() > 0)
                {
                    DataTable result4 = temp4.CopyToDataTable();
                }
    
                Console.ReadLine();
            }
    
        }
    }

    2. 可以返回DataRow数据,避免发生异常

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.Data;
    
    namespace PredicateTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("userid", typeof(int));
                dt.Columns.Add("username", typeof(string));
                dt.Columns.Add("age", typeof(int));
                dt.Rows.Add(1, "test1", 12);
                dt.Rows.Add(2, "test2", 24);
                dt.Rows.Add(3, "test3", 15);
                dt.Rows.Add(4, "test4", 20);
    
                DataRow[] dr = dt.Select("age>1000");
    
                Console.ReadLine();
            }
    
        }
    }
  • 相关阅读:
    任务18格式化
    任务17分区
    任务16 BIOS与CMOS
    任务15硬件组装过程说明
    任务14选配机箱
    任务13选配电源
    任务12选配显卡
    任务11选配机械硬盘
    任务10选配固态硬盘
    Android自定义控件:动画类(八)----ObjectAnimator基本使用
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12889473.html
Copyright © 2011-2022 走看看