C# 資料表篩選器

WBOY
發布: 2024-09-03 15:33:17
原創
234 人瀏覽過

C# DataTable 是一個中心對象,用於存取與資料表相關的大部分對象和資料。由於資料表包含大量資料且沒有組織格式,因此需要套用篩選器。為了滿足C#相關的DataTable中的過濾屬性,需要取得過濾器來對資料進行排列和排序,精簡的C#資料庫過濾器。

文法:

C# DataTable 過濾器沒有特定的語法,但它仍然使用與列關聯的過濾器函數,如下所示:

dataView.RowFilter = "s_id=180";
登入後複製

與文字相關的DataTable過濾函數表示如下:

dataView.RowFilter = "s_name = 'anu'"
登入後複製

與數值相關的DataTable過濾函數表示如下:

dataView.RowFilter = "dt_of_brth = 1987"
登入後複製

如何在C#中過濾資料表?

C#中的濾波函數主要用於資料及其相關運算數量龐大的情況。如果 DataTable 中的資料不斷增加,那麼對於行和列過濾來說唯一的救世主就是 DataTable 中的過濾器。

讓我們檢查一下 C# 中過濾 DataTable 的工作模式:

  • C# 中的過濾 DataTable 並不是唯一的,並且與其他類型的過濾技術不同;儘管如此,它仍然可以透過多種方式來實現。
  • 過濾 DataTable 的方法有多種,包括 select(String) 方法,該方法選擇所需的行或列,然後基於該行或列套用篩選器。
  • 可以使用 Select、Where、AND、OR、NOT 邏輯運算子來完成過濾,並在此基礎上套用值。
  • 資料表中的資料行和列也使用排序方法,根據需要以升序或降序格式對資料進行排序和排序。
  • 選擇字串作為可枚舉在保存任何物件時非常有用,然後根據計算應用過濾和排序操作有助於提供所需的結果。
  • 對 DataTable 及其關聯字串的評估也需要注意 true 或 false 回傳函數。

C# 資料表過濾器範例

下面給出的是 C# DataTable Filter 的範例:

範例#1

程式示範了使用select 語句作為篩選器語句,對於每個AND、OR 和NOT 條件來篩選和取得行數據,並傳回大於所提及的數字但小於其他上限的任何數字,如下所示輸出。

代碼:

using System;
using System.Data;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Data.DataSetExtensions;
public class Data_tbl_Demo
{
public static void Main()
{
DataTable tbl_1 = new DataTable("Creation of Data for players");
tbl_1.Columns.Add(new DataColumn("Size_of_team", typeof(int)));
tbl_1.Columns.Add(new DataColumn("Team_work", typeof(char)));
tbl_1.Rows.Add(50, 'c');
tbl_1.Rows.Add(100, 'c');
tbl_1.Rows.Add(250, 'd');
tbl_1.Rows.Add(567, 'd');
tbl_1.Rows.Add(123, 'd');
DataRow[] rslt = tbl_1.Select("Size_of_team >= 123 AND Team_work = 'd'");
foreach (DataRow row in rslt)
{
Console.WriteLine("{0}, {1}", row[0], row[1]);
}
}
}
登入後複製

輸出:

C# 資料表篩選器

範例#2

此程式用於示範 DataTable 過濾表達式,該表達式用於按降序排序後傳回 DataRow 物件數組,如輸出所示。

代碼:

using System;
using System.Data;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Data.DataSetExtensions;
public class Data_tbl_Demo
{
public static void Main()
{
DataTable tbl2_2 = new DataTable("Orders_plcd");
tbl2_2.Columns.Add("Order_ID", typeof(Int32));
tbl2_2.Columns.Add("Order_Quantity", typeof(Int32));
tbl2_2.Columns.Add("Company_Name", typeof(string));
tbl2_2.Columns.Add("Date_on_day", typeof(DateTime));
DataRow nw_row = tbl2_2.NewRow();
nw_row["Order_ID"] = 1;
nw_row["Order_Quantity"] = 5;
nw_row["Company_Name"] = "New_Company_Nm";
nw_row["Date_on_day"] = "2014, 5, 25";
tbl2_2.Rows.Add(nw_row);
DataRow nw_row2 = tbl2_2.NewRow();
nw_row2["Order_ID"] = 2;
nw_row2["Order_Quantity"] = 6;
nw_row2["Company_Name"] = "New_Company_Nm2";
tbl2_2.Rows.Add(nw_row2);
DataRow nw_row3 = tbl2_2.NewRow();
nw_row3["Order_ID"] = 3;
nw_row3["Order_Quantity"] = 8;
nw_row3["Company_Name"] = "New_Company_Nm3";
tbl2_2.Rows.Add(nw_row3);
string exprsn = "Date_on_day = '5/25/2014' or Order_ID = 2";
string sort_Order = "Company_Name DESC";
DataRow[] sorted_Rows;
sorted_Rows = tbl2_2.Select(exprsn, sort_Order);
for (int i = 0; i < sorted_Rows.Length; i++)
Console.WriteLine(sorted_Rows[i][2]);
}
}
登入後複製

輸出:

C# 資料表篩選器

範例#3

此程式示範了選擇查詢,其中 DataTable 會尋找兩個符合行,這些行的日期採用較新的格式,並使用 DateTime 進行過濾,如輸出所示。

代碼:

using System;
using System.Data;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Data.DataSetExtensions;
public class Using_Date_Time
{
public static void Main()
{
DataTable tbl_dt_time = new DataTable("Widgets");
tbl_dt_time.Columns.Add(new DataColumn("rw_ID", typeof(int)));
tbl_dt_time.Columns.Add(new DataColumn("Date", typeof(DateTime)));
tbl_dt_time.Rows.Add(180, new DateTime(2003, 1, 1));
tbl_dt_time.Rows.Add(123, new DateTime(2000,1, 1));
tbl_dt_time.Rows.Add(350, new DateTime(2001,1, 1));
DataRow[] filterd_result = tbl_dt_time.Select("Date > #6/1/2001#");
foreach (DataRow row in filterd_result)
{
Console.WriteLine(row["rw_ID"]);
}
}
}
登入後複製

輸出:

C# 資料表篩選器

範例#4

程式透過選擇像 A 這樣的值來說明無效表達式,該值不會被評估為 true 或 false,並會拋出一個不希望出現的令人討厭的錯誤。

代碼:

using System;
using System.Data;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Data.DataSetExtensions;
public class Using_Date_Time
{
public static void Main()
{
DataTable table = new DataTable();
table.Columns.Add("Anusua", typeof(int));
table.Rows.Add(1);
table.Rows.Add(2);
table.Rows.Add(3);
table.Rows.Add(4);
table.Rows.Add(5);
DataRow[] rows = table.Select("Anusua");
System.Console.WriteLine(rows.Length);
}
}
登入後複製

輸出:

C# 資料表篩選器

注意:為了克服上述評估資料表和透過評估值過濾資料表的情況,請正確涉及這組語句。

語句包含以下行:

DataRow[] rows = table.Select(“Anusua > 1”);

System.Console.WriteLine(rows.Length);

如果透過取代前面提到的範例中的 select 語句正確執行,上面兩行將提供所需的輸出。

輸出結果為:

C# 資料表篩選器

範例#5

程式示範了要過濾的資料表並執行求和操作,該操作將駐留在建立為求和的物件內,從中取得所需的總和並顯示在輸出中。

代碼:

using System;
using System.Data;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Data.DataSetExtensions;
public class Program
{
public static void Main()
{
DataTable dt_4 = new DataTable();
dt_4.Columns.Add("emp_Id",typeof(int));
dt_4.Columns.Add("customer_Name",typeof(string));
dt_4.Columns.Add("Amount_type",typeof(decimal));
dt_4.Rows.Add(1,"A",50);
dt_4.Rows.Add(2,"b",68);
dt_4.Rows.Add(3,"c",22);
dt_4.Rows.Add(4,"d",null);
decimal dec_ml = 0;
object sum_Obj;
sum_Obj = dt_4.Compute("Sum(Amount_type)", string.Empty);
decimal total = dt_4.AsEnumerable().Where(r => !r.IsNull("Amount_type") && decimal.TryParse(r["Amount_type"].ToString(), out dec_ml)).Sum(r => dec_ml);
Console.WriteLine(sum_Obj);
Console.WriteLine(total);
}
}
登入後複製

輸出:

C# 資料表篩選器

結論

C# 和任何其他程式語言中的 DataTable 在處理大量資料時都發揮關鍵作用。針對資料庫及其後續子集的過濾也發揮著重要作用,因為資料庫在從資料庫取得和檢索資料方面應始終優化且有效率。

以上是C# 資料表篩選器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!