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});
}
}
if (ds!= null)
Ds. Tables.add (DT);
return DT;
}
"Outh24":
It would be nice if you had LINQ ...
If not, there is a stupid way to define another DataTable,
Write a duplicate portion of the current data source into the new DT
"fuda_1985":
Concern.
"SCJYJL":
Did not find columnequal, I would like to have a using what this ah, and I have no index in MSDN, you can tell?
"Ezhuyin":
Idea: First create a temporary data table, then sort the source DataTable, and then read the source data line-by-row, and compare it to the last time you added it, and if it is the same, add it to the temporary table if it is the same. Make a small change, you can adapt to more environment.
Public DataTable selectdistinct (String tablename, DataTable SourceTable, string[] fieldnames)
{
DataTable dt = new DataTable (tablename);
System.Text.StringBuilder Filednamebuilder = new System.Text.StringBuilder ("");
String sortstr = "";
object[] Lastvalue = new Object[fieldnames.length];
foreach (DataRow Dr in Sourcetable.select ("", Sortstr))
{
if (Lastvalue = NULL | |!) IsEqual (Dr,lastvalue))
{
for (int i = 0; i < lastvalue.length; i++)
{
Lastvalue[i] = dr[fieldnames[i]];
}
Dt. Rows.Add (Lastvalue);
}
}
if (ds!= null)
Ds. Tables.add (DT);
return DT;
}
private bool IsEqual (DataRow Dr, object[] Value)
{
bool Equal = true;
int current = 0;
while (Equal && current < Value.length)
{
if (!dr[current). Equals (Value[current])
{
Equal = false;
}
Else
{
current++;
}
}
return Equal;
}
"Ezhuyin":
Did not find columnequal, I would like to have a using what this ah, and I have no index in MSDN, you can tell?
Ah, sorry, that's a separate method:
private bool Columnequal (object A, Object B)
{
if (a = = DBNull.Value && B = dbnull.value)//two are DBNull.Value
return true;
if (A = = DBNull.Value | | B = = DBNull.Value)//Only one is DBNull.Value
return false;
Return (A.equals (B)); Normal comparison
}
"Ezhuyin":
if (!dr[current). Equals (Value[current])
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.