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();
        }

    }
}
posted @ 2020-05-14 16:20  LuckyZLi  阅读(1118)  评论(0编辑  收藏  举报