DataTable.Select 方法

來源:互聯網
上載者:User

DataTable.Select方法返回擷取所有DataRow對象的數組。Select()有四個重載方法.

  名稱 說明
Select () 擷取所有 DataRow 對象的數組。
Select(String) 按照主鍵順序(如果沒有主鍵,則按照添加順序)擷取與篩選條件相匹配的所有 DataRow 對象的數組。
Select(String, String) 擷取按照指定的排序次序且與篩選條件相匹配的所有 DataRow  對象的數組。
Select(String, String, DataViewRowState) 擷取與排序次序中的篩選器以及指定的狀態相匹配的所有 DataRow  對象的數組。

 

1.Select()樣本:

 private
void GetRows()
{
    // Get the DataTable of a DataSet.
    DataTable table = DataSet1.Tables["Suppliers"];
    DataRow[] rows = table.Select();

    // Print the value one column of each DataRow.
    for(int i = 0; i < rows.Length ; i++)
    {
        Console.WriteLine(rows[i]["CompanyName"]);
    }
}

 

2.Select(string
filterExpression)樣本:

 private
void GetRowsByFilter()
{
    DataTable table = DataSet1.Tables["Orders"];
    // Presuming the DataTable has a column named Date.
    string expression;
    expression = "Date > #1/1/00#";
    DataRow[] foundRows;

    // Use the Select method to find all rows matching the filter.
    foundRows = table.Select(expression);

    // Print column 0 of each returned row.
    for(int i = 0; i < foundRows.Length; i ++)
    {
        Console.WriteLine(foundRows[i][0]);
    }
}

3.Select(string filterExpression,string sort)樣本:

    private
void GetRowsByFilter()
   {
       DataTable table = DataSet1.Tables["Orders"];

       // Presuming the DataTable has a column named Date.
       string expression =
"Date > '1/1/00'";

       // Sort descending by column named CompanyName.
       string sortOrder =
"CompanyName DESC";
       DataRow[] foundRows;

       // Use the Select method to find all rows matching the filter.
       foundRows = table.Select(expression, sortOrder);

      // Print column 0 of each returned row.
       for(int i = 0; i < foundRows.Length; i ++)
       {
           Console.WriteLine(foundRows[i][0]);
       }
   }
4.Select(string filterExpression,string sort, DataViewRowState
recordStates)樣本:

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.