I'm having some issues trying to retrieve unique values from a DataSet in csharp, is that possible?
Actually I'm doing something like this that gets a dataset from a webservice:
webService.getInstructions(Username, Password, AppKey).Tables[0].Select(null, "account name asc");
So in this case I get a alphabetical list from the accounts, but there are some duplicated rows in this dataset.
Is there any way to make this Dataset return values with unique "account number" and sort it alphabetically by "account name"?
Something in place of the filterExpression would be very nice I think. :)
Thanks in advance
I would use a little linq magic on the datatable.
With the results printed to the screen, notice that any row with an AccountNumber of '5' is missing, because it wasn't unique. Also notice that in the first example i used the dataTable.Select() to do the ordering, as the ordering is the same irrespective of what rows are removed due to not being unique. The second and third samples will give you an IEnumerable list of rows to work with that you can bind directly to, the first will give you a bunch of groups containing the individual rows.
Personally I'd change the web-service to do this filtering and sorting at the server to reduce bandwidth needs, probably returning a simple data-type or custom class (not
DataTable
or anything similar). But LINQ would do the job... (updated after re-reading the question)using custom
DistinctBy
method: