6

How can I retrieve all elements in a List ignoring duplicate entries

So if List A contains:

Employee A
Employee A 
Employee B 
Employee B
Employee C 
Employee D   
Employee E

I want to get

Employee A
Employee B 
Employee C 
Employee D
Employee E
  • 1
    This is a really unclear question, and the answers you are likely to get are going to be all over the map. If you explain your requirement in a little more detail you are likely to get more useful answers. Who knows, maybe you can achieve what you want without using CAML at all! – Derek Gusoff Sep 25 '13 at 14:32
  • ...and please include the SharePoint version you are using...that matters a lot. – Derek Gusoff Sep 25 '13 at 14:32
9

Daniel,

The Distinct operation isn't available in CAML Queries.. However, there are different things you can try.. You can use LINQ to get distinct results:

var distinctItems = (from SPListItem item in items select item["EmployeeName"]).Distinct().ToArray();

Or convert your results to DataView and do something like:

SPList oList = SPContext.Current.Web.Lists["ListName"];
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='Name' /></OrderBy>";
DataTable dtcamltest = oList.GetItems(query).GetDataTable();
DataView dtview = new DataView(dtcamltest);
DataTable dtdistinct = dtview.ToTable(true, "Name");
7

CAML query schema for SP2013 doesn't contain distinct still, so I think it is not changed. However you may group results in CAML (using GroupBy element) - it will produce groups for distinct values of the field.

Ref: http://social.technet.microsoft.com/Forums/sharepoint/en-US/c2bbcf13-a53d-4755-87dd-871a51840a4d/select-distinct-in-caml-query-for-list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.