菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
37
0

LINQ使用CopyToDataTable

原创
05/13 14:22
阅读数 52645

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

    }
}

发表评论

0/200
37 点赞
0 评论
收藏