SharePoint SPQuery Obtaining Distinct Results

To my knowledge (and someone please post and correct me if I’m wrong) there doesn’t seem to be a solid way to build an SPQuery object that can return distinct values from a list. I’ve seen some clever tricks for getting around this particular problem, but nothing quite like this.

If you dig through this post a little bit, you’ll find the technique to getting distinct values comes down to leveraging a DataView.ToTable() method to return only distinct results.

So let’s break this down a bit. A typical scenario for me is to build a data table that contains all of my data (via an SPQuery to the list), set the data source of my control to that data table and then bind.

(In my case, I work specifically with the Telerik RadGrid and the Telerik RadComboBox but the same idea applies to the standard ASP.NET controls)

DataTable groupedDataTable = originalDataTable.DefaultView.ToTable(true, "Title")
dataGrid.DataSource = groupedDataTable;

The ToTable() method accepts two parameters as demonstrated above.

  1. bool distinct simply indicates whether or not to supply distinct values
  2. params string[] columnNames is a string array that contains a list of the column names in the orignal data table that you’d like to be returned
This worked quite nicely for me, a huge thank you to the original author of the referenced post.


No comments yet.

Leave a Reply