Jak wybrać odrębne wiersze w datatable i zapisać w tablicy
Mam zbiór danych. obiekt zawiera tabelę o nazwie Table1. Table1 zawiera kolumnę o nazwie ProcessName. Nazwa procesu zawiera powtarzane names.So chcę wybrać tylko różne names.Is to możliwe.
intUniqId[i] = (objds.Tables[0].Rows[i]["ProcessName"].ToString());
17 answers
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-29 12:37:51
Po jednym wierszu kodu unikniemy duplikatów wierszy DataTable
:
dataTable.DefaultView.ToTable(true, "employeeid");
Gdzie:
Pierwszy parametr
ToTable()
to boolean, który wskazuje, czy chcesz mieć oddzielne wiersze, czy nie.Drugi parametr w
ToTable()
to nazwa kolumny, na podstawie której musimy wybrać odrębne wiersze. Tylko te kolumny będą w zwracanym datatable.
To samo można zrobić z DataSet
, poprzez dostęp do określonego DataTable
:
dataSet.Tables["Employee"].DefaultView.ToTable(true, "employeeid");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-26 11:38:44
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();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-29 10:14:51
Z 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); }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-29 05:51:47
Możesz użyć Tak:
data
is DataTable
data.DefaultView.ToTable(true, "Id", "Name", "Role", "DC1", "DC2", "DC3", "DC4", "DC5", "DC6", "DC7");
Ale wydajność spadnie. spróbuj użyć poniższego kodu:
data.AsEnumerable().Distinct(System.Data.DataRowComparer.Default).ToList();
Za wykonanie; http://onerkaya.blogspot.com/2013/01/distinct-dataviewtotable-vs-linq.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-07 14:12:59
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();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-19 23:33:08
Aby poprawić powyższą odpowiedź: funkcja ToTable w dataview ma flagę "distinct".
//This will filter all records to be distinct
dt = dt.DefaultView.ToTable(true);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-26 19:40:02
Następujące prace. Mam to działa dla mnie z. 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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-02 11:41:22
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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-03 16:17:52
I just happened to find this: http://support.microsoft.com/default.aspx?scid=kb; en-us;326176#1
Szukając czegoś podobnego, tylko specjalnie dla. Net 2.0
Im zakładając, że OP szukał odrębnych podczas korzystania z DataTable.Select (). (Select () nie obsługuje distinct)
Oto kod z powyższego linku:
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
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-02 11:41:47
var ValuetoReturn = (from Rows in YourDataTable.AsEnumerable()
select Rows["ColumnName"]).Distinct().ToList();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-04 07:36:43
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 };
Filtruje tabelę danych za pomocą eecode i taxyear łącznie uważane za unikalne
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-03 16:17:31
Składnia:-
DataTable dt = ds.Tables[0].DefaultView.ToTable(true, "ColumnName");
EX:-
DataTable uniqueCols = dsUDFlable.Tables[0].DefaultView.ToTable(true, "BorrowerLabelName");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-03 16:18:04
Najprostszym rozwiązaniem jest użycie linq, a następnie przekształcenie wyniku w DataTable
//data is a DataTable that you want to change
DataTable result = data.AsEnumerable().Distinct().CopyToDataTable < DataRow > ();
To jest ważne na asp.net 4.0 ^ Framework jeśli sobie przypomnę.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-31 14:30:28
It is easy
DataView view = new DataView(dt);
DataTable dt2 = view.ToTable(true, "Column1", "Column2","Column3", ...,"ColumnNth");
I DT2 datatable zawierają column1, Column2..Unikalne dane w kolumnie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-16 10:05:28
Sthing like ?
SELECT DISTINCT .... Z tabeli gdzie warunek
Http://www.felixgers.de/teaching/sql/sql_distinct.html
Uwaga: zadanie domowe pytanie ? i niech Bóg błogosławi google..
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-29 10:14:22
DataTable dtbs = new DataTable();
DataView dvbs = new DataView(dt);
dvbs.RowFilter = "ColumnName='Filtervalue'";
dtbs = dvbs.ToTable();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-07 10:30:03