zoukankan      html  css  js  c++  java
  • DataTable.Select 方法 (String, String, DataViewRowState)

    public DataRow[] Select (
     string filterExpression,
     string sort,
     DataViewRowState recordStates
    )

    参数

    filterExpression

    要用来筛选行的条件。

    sort

    一个字符串,它指定列和排序方向。

    recordStates

    DataViewRowState 值之一。

    返回值

    DataRow 对象的数组。

    示例

    private static void GetRowsByFilter()
    {
        DataTable customerTable = new DataTable("Customers");
        // Add columns
        customerTable.Columns.Add("id", typeof(int));
        customerTable.Columns.Add("name", typeof(string));

        // Set PrimaryKey
        customerTable.Columns[ "id" ].Unique = true;
        customerTable.PrimaryKey = new DataColumn[]
            { customerTable.Columns["id"] };

        // Add ten rows
        for(int id=1; id<=10; id++)
        {
            customerTable.Rows.Add(
                new object[] { id, string.Format("customer{0}", id) });
        }
        customerTable.AcceptChanges();

        // Add another ten rows
        for(int id=11; id<=20; id++)
        {
            customerTable.Rows.Add(
                new object[] { id, string.Format("customer{0}", id) });
        }

        string expression;
        string sortOrder;

        expression = "id > 5";
        // Sort descending by column named CompanyName.
        sortOrder = "name DESC";
        // Use the Select method to find all rows matching the filter.
        DataRow[] foundRows =
            customerTable.Select(expression, sortOrder,
            DataViewRowState.Added);

        PrintRows(foundRows, "filtered rows");

        foundRows = customerTable.Select();
        PrintRows(foundRows, "all rows");
    }

    private static void PrintRows(DataRow[] rows, string label)
    {
        Console.WriteLine("\n{0}", label);
        if(rows.Length <= 0)
        {
            Console.WriteLine("no rows found");
            return;
        }
        foreach(DataRow row in rows)
        {
            foreach(DataColumn column in row.Table.Columns)
            {
                Console.Write("\table {0}", row[column]);
            }
            Console.WriteLine();
        }
    }

    ------------------------------------------------------------------------------------------------------------

    DataTable是我们在进行开发时经常用到的一个类,并且经常需要对DataTable中的数据进行筛选等操作,下面就介绍一下Datatable中经常用到的一个方法——Select,微软提供了四个函数的重载,分别是

    Select()

    Select(string filterExpression)

    Select(string filterExpression, string sort)

    Select(string filterExpression,string sort, DataViewRowState record States)。

    1)  Select()——获取所有 System.Data.DataRow 对象的数组。

    2)  Select(string filterExpression)——按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 System.Data.DataRow 对象的数组。

    3)  Select(string filterExpression, string sort)——获取按照指定的排序顺序且与筛选条件相匹配的所有 System.Data.DataRow 对象的数组。

    4)  Select(string filterExpression, string sort, DataViewRowState recordStates)——获取与排序顺序中的筛选器以及指定的状态相匹配的所有 System.Data.DataRow 对象的数组。

    下面是对这些方法进行演示的示例:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Data;

     

    namespace TestDataTableSelect

    {

        class Program

        {

            static DataTable dt = new DataTable();

            static void Main(string[] args)

            {         

                DataColumn dc1 = new DataColumn("id");

                dc1.DataType=typeof(int);

                DataColumn dc2 = new DataColumn("name");

                dc2.DataType=typeof(System.String);

                dt.Columns.Add(dc1);

                dt.Columns.Add(dc2);

                for (int i = 1; i <=10;i++ )

                {

                    DataRow dr = dt.NewRow();

                    if (i <= 5)

                    {

                        dr[0] = i;

                        dr[1] = i + "--" + "hello";

                    }

                    else

                    {

                        dr[0] = i;

                        dr[1] = i + "--" + "nihao";

                    }

                    dt.Rows.Add(dr);

                }

     

                Select();

                Select("id>='3' and name='3--hello'");//支持and

                Select("id>='3' or id='1'");//支持or

                Select("name like '%hello%'");//支持like   

                Select("id>5","id desc");

                Select("id>5", "id desc",DataViewRowState.Added);

            }

     

            private static void Select()

            {

                DataRow[] arrayDR = dt.Select();

                foreach(DataRow dr in arrayDR)

                {

                    Console.WriteLine(dr[0].ToString()+"    "+dr[1].ToString());

                }

                Console.ReadLine();

            }

     

            private static void Select(string filterExpression)

            {

                DataRow[] arrayDR = dt.Select(filterExpression);

                foreach (DataRow dr in arrayDR)

                {

                    Console.WriteLine(dr[0].ToString() + "    " + dr[1].ToString());

                }

                Console.ReadLine();

            }

     

            private static void Select(string filterExpression, string sort)

            {

                DataRow[] arrayDR = dt.Select(filterExpression,sort);

                foreach (DataRow dr in arrayDR)

                {

                    Console.WriteLine(dr[0].ToString() + "    " + dr[1].ToString());

                }

                Console.ReadLine();

            }

     

            private static void Select(string filterExpression, string sort, DataViewRowState recordStates)

            {

                DataRow[] arrayDR = dt.Select(filterExpression, sort,recordStates);

                foreach (DataRow dr in arrayDR)

                {

                    Console.WriteLine(dr[0].ToString() + "    " + dr[1].ToString());

                }

                Console.ReadLine();

            }

        }

    }

     注意事项:上面的Select操作是大小写不敏感的(记录的字段不敏感),如果需要区分大小写,需要将DataTable的caseSensitive属性设为true。

  • 相关阅读:
    如何对一台服务器进行免密操作
    MySQL 中的自增主键
    mysql 添加多版本数据库
    Group by 优化
    join 查询优化
    CCF2020-09-Python题解
    CCF2019-09-Python题解
    CCF2019-12-Python题解
    CCF202006-Python题解
    差分约束
  • 原文地址:https://www.cnblogs.com/Tally/p/2660203.html
Copyright © 2011-2022 走看看