我有一個數據集objds。 objds包含一個名爲Table1的表。表1包含名爲ProcessName的列。此ProcessName包含重複的名稱。因此,我想只選擇不同的名稱。這是可能的。如何在數據表中選擇不同的行並存儲到數組中
intUniqId[i] = (objds.Tables[0].Rows[i]["ProcessName"].ToString());
我有一個數據集objds。 objds包含一個名爲Table1的表。表1包含名爲ProcessName的列。此ProcessName包含重複的名稱。因此,我想只選擇不同的名稱。這是可能的。如何在數據表中選擇不同的行並存儲到數組中
intUniqId[i] = (objds.Tables[0].Rows[i]["ProcessName"].ToString());
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...);
sthing like?
SELECT DISTINCT .... FROM表WHERE條件
http://www.felixgers.de/teaching/sql/sql_distinct.html
注:家庭作業的問題?和神保佑谷歌..
向誰低估了我:S,顯然這個問題在我的回答後被修改了? (回答10:15,12:15編輯的問題)哦,好吧.. thx爲你的無知:) – 2009-10-22 08:16:32
OP詢問如何在C#ado.net環境中選擇不同的行,而不是在實際的數據庫中。 – aggaton 2016-03-02 21:49:26
DataTable dt = new DataTable();
dt.Columns.Add("IntValue", typeof(int));
dt.Columns.Add("StringValue", typeof(string));
dt.Rows.Add(1, "1");
dt.Rows.Add(1, "1");
dt.Rows.Add(1, "1");
dt.Rows.Add(2, "2");
dt.Rows.Add(2, "2");
var x = (from r in dt.AsEnumerable()
select r["IntValue"]).Distinct().ToList();
爲了改善上述答案:上數據視圖的ToTable函數具有 「不同的」 標誌。
//This will filter all records to be distinct
dt = dt.DefaultView.ToTable(true);
我只是偶然發現這一點: http://support.microsoft.com/default.aspx?scid=kb;en-us;326176#1
在尋找類似的,只是,專門針對.NET 2.0
林假設的東西在OP一直在尋找不同的,同時使用數據表。選擇()。 (選擇()不支持不同)
因此,這裏是從上面的鏈接代碼:
class DataTableHelper
{
public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
{
DataTable dt = new DataTable(TableName);
dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
object LastValue = null;
foreach (DataRow dr in SourceTable.Select("", FieldName))
{
if ( LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
{
LastValue = dr[FieldName];
dt.Rows.Add(new object[]{LastValue});
}
}
return dt;
}
private bool ColumnEqual(object A, object B)
{
// Compares two values to see if they are equal. Also compares DBNULL.Value.
// Note: If your DataTable contains object fields, then you must extend this
// function to handle them in a meaningful way if you intend to group on them.
if (A == DBNull.Value && B == DBNull.Value) // both are DBNull.Value
return true;
if (A == DBNull.Value || B == DBNull.Value) // only one is DBNull.Value
return false;
return (A.Equals(B)); // value type standard comparison
}
}
下面的一行代碼將避免DataTable
的重複行:
dataTable.DefaultView.ToTable(true, "employeeid");
其中:
ToTable()
第一個參數是布爾型它指示您是否需要不同的行。
ToTable()
中的第二個參數是列名,我們必須選擇不同的行。只有這些列將在返回的數據表中。
同樣可以從DataSet
來完成,通過訪問特定DataTable
:
dataSet.Tables["Employee"].DefaultView.ToTable(true, "employeeid");
隨着LINQ(.NET 3.5,C#3)
var distinctNames = (from row in DataTable.AsEnumerable()
select row.Field<string>("Name")).Distinct();
foreach (var name in distinctNames) { Console.WriteLine(name); }
var distinctRows = (from DataRow dRow in dtInventory.Rows
select dRow["column_name"]).Distinct();
var distinctRows = (from DataRow dRow in dtInventory.Rows
select dRow["col1"], dRow["col2"]..).Distinct();
繼作品。我使用.NET 3爲我工作。5 SP1
// Create the list of columns
String[] szColumns = new String[data.Columns.Count];
for (int index = 0; index < data.Columns.Count; index++)
{
szColumns[index] = data.Columns[index].ColumnName;
}
// Get the distinct records
data = data.DefaultView.ToTable(true, szColumns);
var ValuetoReturn = (from Rows in YourDataTable.AsEnumerable()
select Rows["ColumnName"]).Distinct().ToList();
您可以使用這樣的:
data
是DataTable
data.DefaultView.ToTable(true, "Id", "Name", "Role", "DC1", "DC2", "DC3", "DC4", "DC5", "DC6", "DC7");
但性能會有所回落。嘗試使用以下代碼:
data.AsEnumerable().Distinct(System.Data.DataRowComparer.Default).ToList();
對於性能; http://onerkaya.blogspot.com/2013/01/distinct-dataviewtotable-vs-linq.html
語法: -
DataTable dt = ds.Tables[0].DefaultView.ToTable(true, "ColumnName");
EX: -
DataTable uniqueCols = dsUDFlable.Tables[0].DefaultView.ToTable(true, "BorrowerLabelName");
string[] TobeDistinct = {"Name","City","State"};
DataTable dtDistinct = GetDistinctRecords(DTwithDuplicate, TobeDistinct);
//Following function will return Distinct records for Name, City and State column.
public static DataTable GetDistinctRecords(DataTable dt, string[] Columns)
{
DataTable dtUniqRecords = new DataTable();
dtUniqRecords = dt.DefaultView.ToTable(true, Columns);
return dtUniqRecords;
}
DataTable dt = new DataTable("EMPLOYEE_LIST");
DataColumn eeCode = dt.Columns.Add("EMPLOYEE_CODE", typeof(String));
DataColumn taxYear = dt.Columns.Add("TAX_YEAR", typeof(String));
DataColumn intData = dt.Columns.Add("INT_DATA", typeof(int));
DataColumn textData = dt.Columns.Add("TEXT_DATA", typeof(String));
dt.PrimaryKey = new DataColumn[] { eeCode, taxYear };
它過濾數據表eecode和taxyear結合起來視爲唯一
DataTable dtbs = new DataTable();
DataView dvbs = new DataView(dt);
dvbs.RowFilter = "ColumnName='Filtervalue'";
dtbs = dvbs.ToTable();
最簡單的解決方法是使用LINQ,然後將結果轉換爲一個DataTable
//data is a DataTable that you want to change
DataTable result = data.AsEnumerable().Distinct().CopyToDataTable <DataRow>();
這是有效 爲asp.net 4.0 ^框架 如果我remeber。
從您在下面做出的評論中發佈示例代碼,看起來答案取決於您正在使用的查詢的具體內容。 – MatthewMartin 2009-07-29 12:23:24