Wednesday, November 23, 2011

How to get distinct value from splistitemcollection

In many cases, we need to get the distinct data like SQL or even only get the separately data. it will be complicated if we are working on SharePoint 2010 because of performance. There are at least 2 scenarios we have:

  • Max throttling 
  • Performance with large list
Now, i also have a small code to get the data for distinct:


SPListItemCollection collection = list.Items;
DataView view = new DataView(collection.GetDataTable());
DataTable result = view.ToTable(true, "[Column Name"]);


By using DataView and DataTable we can get the distinct value from list, but if it's a large list you only get Max Throtting items in a collection, others will be missing. The most important thing is this way is easy to use.

IEnumerable enumerable = list.Items.Cast<SPListItem>().Select(itm => itm["Column Name"]).Distinct();

Or you can use Linq to select the distinct value, this way will not be recommend because of performance but it will return exactly data if the maximum return records were not exceeded the Max Throttling.

It's up to you to choose any way, but hope this help.

No comments: