How to select and record No duplicates in a DataTable? _null

Source: Internet
Author: User
Just like the SELECT distinct field name from table name
What to do with the select in the DataTable?


Reply to the content "wwwiii520":
Occupy a Sand "" "do not understand LZ meaning

"Ezhuyin":
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});
}
}
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 = "";

foreach (String FieldName in FieldNames)
{
Dt. Columns.Add (FieldName, Sourcetable.columns[fieldname). DataType);
Filednamebuilder.append (FieldName + ",");
}

if (filednamebuilder.tostring (). LENGTH>0)
{
Sortstr = Filednamebuilder.tostring ();
Sortstr = sortstr.substring (0,sortstr.length-1);
}

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

Another mistake, modified into

if (! Columnequal (Dr[current],value[current])

"SCJYJL":
Thanks, Ezhuyin.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.