I am working on a legacy project which was developed using .NET Framework 2.0.
In this project, I get distinct values from DataRowCollection
by ItemNo
column. I am only interested in ItemNo
. The DataRow
consist of ItemNo
, Qty
and Date
.
I am thinking of iterating the DataRowCollection
and adding the unique ItemNo
into a list of string as below (not tested)
var items = new List<string>();
foreach (DataRow orderItem in rows)
{
var itemNo = orderItem["ITEMNO"].ToString().Trim();
if(items.Find(delegate(string str) { return str == itemNo ;}) == null)
{
items.Add(itemNo);
}
}
Is there a better way of doing this without LINQ (.Net Framework 2.0 doesnt like LINQ)
To get distinct values form a column you can use this method:
In above method I used
DataView.ToTable
which by passingtrue
as first argument, selects distinct values.Here is the usage example:
Note
If you need to trim values, you can change above code and first create a clone copy of the
DataTable
. Then add a computed column which contains, trimmed value from the given column name for distinct values by assigningTRIM(column)
toExpression
property of column. Then follow the steps using the new trimmed column like above code.If you can install .Net 3.5 on the server, and reference System.Core.dll in your application, you can leverage HashSets which would modify the above code to:
The benefit of using HashSet over a Dictionary is admittedly trivial, but I'd prefer it since I don't care for the arbitrary bool value in the dictionary, but you'd need to meet the .Net 3.5 and reference requisites.