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();
            }
    
        }
    }
  • 相关阅读:
    USACO 5.1 Starry Night
    USACO 4.4 Frame Up
    USACO 4.4 Shuttle Puzzle
    USACO 4.3 Letter Game (字典树)
    USACO 4.3 Street Race
    BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)
    BZOJ 1861: [Zjoi2006]Book 书架 (splay)
    codeforces 354 D. Transferring Pyramid
    codeforces 286 E. Ladies' Shop (FFT)
    USACO 4.3 Buy Low, Buy Lower
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12889473.html
Copyright © 2011-2022 走看看