C# DataTable.Select DataRow [SQL 二次搜尋]

C# DataTable.Select DataRow [SQL 二次搜尋]

C# DataTable.Select DataRow [SQL 二次搜尋]


資料來源: https://learn.microsoft.com/zh-tw/dotnet/api/system.data.datatable.select?view=net-7.0


Code01

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


Code02[無SQL+過濾+排序]

using System;
using System.Data;

public class A {

   public static void Main() {
      DataTable table = new DataTable("Orders");
      table.Columns.Add("OrderID", typeof(Int32));
      table.Columns.Add("OrderQuantity", typeof(Int32));
      table.Columns.Add("CompanyName", typeof(string));
      table.Columns.Add("Date", typeof(DateTime));

      DataRow newRow = table.NewRow();
      newRow["OrderID"] = 1;
      newRow["OrderQuantity"] = 3;
      newRow["CompanyName"] = "NewCompanyName";
      newRow["Date"] = "1979, 1, 31";

      // Add the row to the rows collection.
      table.Rows.Add(newRow);

      DataRow newRow2 = table.NewRow();
      newRow2["OrderID"] = 2;
      newRow2["OrderQuantity"] = 2;
      newRow2["CompanyName"] = "NewCompanyName1";
      table.Rows.Add(newRow2);

      DataRow newRow3 = table.NewRow();
      newRow3["OrderID"] = 3;
      newRow3["OrderQuantity"] = 2;
      newRow3["CompanyName"] = "NewCompanyName2";
      table.Rows.Add(newRow3);

      // Presuming the DataTable has a column named Date.
      string expression = "Date = '1/31/1979' or OrderID = 2";
      // string expression = "OrderQuantity = 2 and OrderID = 2";

      // Sort descending by column named CompanyName.
      string sortOrder = "CompanyName ASC";
      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][2]);
   }
}

One thought on “C# DataTable.Select DataRow [SQL 二次搜尋]

  1. C# DataTable.Select DataRow [SQL 二次搜尋]
    無 SQL 資料庫 搜尋/尋找 取代方案

    C# DataTable中執行DataTable.Select(“條件”)

    https://www.twblogs.net/a/5b9215ec2b71772002d26b4d

    Select方法:
     Select();//全部查出來
     Select(過濾條件);//根據過濾條件進行過濾,如Select(“columnname1 like ‘%xx%'”);
     Select(過濾條件,排序字段);//過濾,並排序,如Select(“columnname1 like ‘%xx%'”,columnname2);

    00.支援SQL IN 語法
    01.支援SQL LIKE 語法
    02.支援SQL關係運算式 >,=,<=,!=,=
    03.支援SQL邏輯運算式 AND,OR
    04.支援四則運算式
    PS.不支援 : BETWEEN AND


    //DataTable.Select("條件") 返回 DataTable
    DataTable newdt = new DataTable();
    newdt = dt.Clone(); // 克隆dt 的結構,包括所有 dt 架構和約束,並無數據;
    DataRow[] rows = dt.Select(conditions); // 從dt 中查詢符合條件的記錄;
    foreach (DataRow row in rows) // 將查詢的結果添加到dt中;
    {
    newdt.Rows.Add(row.ItemArray);
    }

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *