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();
            }
    
        }
    }
  • 相关阅读:
    Navicat Premium 12.1.12.0破解版激活
    vConsole调试器
    使用DbFunctions来解决asp.net mvc ef按照日期分组数据
    谷歌浏览器如何安装CRX插件?crx离线插件安装方法
    ASP.NET MVC——CodeFirst开发模式
    Sql server 事务的两种用法
    SQL Server 存储过程
    JqueryMobile新手问题大全
    .net core 轻量级容器 ServiceProvider 源码分析
    Centos7 使用Docker 部署mssql 2017
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12889473.html
Copyright © 2011-2022 走看看