Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have got a list with names of workers. How could I populate a DropDownList with unique values (ie: avoid duplicate) from the list? If the list contains duplicate values I need to avoid inserting the same value multiple times in the dropdown.

Here is my current code:

SPList kontraktyList = witryna.Lists["Urlopy_prac"];
SPQuery kontraktyQuery = new SPQuery();
kontraktyQuery.Query = "<Query><OrderBy><FieldRef Name='nazwisko'/></OrderBy></Query>";
SPListItemCollection kontraktyGrupa = kontraktyList.GetItems(kontraktyQuery);

foreach (SPListItem kontraktyListItem in kontraktyGrupa)
{
    DropDownList4.Items.Add(kontraktyListItem["nazwisko"].ToString());
}
share|improve this question
3  
Hi Grzegorz - I don't think you want <Query> in your Query... whenever I put that in my CAML it stops it working and just returns all results without any mind paid to the query. –  TZHX Dec 12 '12 at 13:04
    
I want only take one field from the External List. How Would it be fasters, and oriented Ascending ? Only one field, but ordered Ascending –  Grzegorz Z Dec 12 '12 at 13:25
1  
If you want your CAML query to return only 1 field, please use: kontraktyQuery.ViewFields = "<FieldRef Name='nazwisko'/>";. No sense to use the .Query property to restrict the number of fields to be returned. The .Query property is for <Where/> and <OrderBy/> clauses. –  Norbert Dec 12 '12 at 14:11
add comment

1 Answer 1

up vote 1 down vote accepted

I would modify your foreach like this:

foreach (string kontraktyListText in kontraktyGrupagreat.Cast<SPListItem>().Select<SPListItem,string>(source => source["nazwisko"] as string).Distinct())
{
    DropDownList4.Items.Add(kontraktyListText);
}

Please try it out and tell me if it worked.

Cheers!

share|improve this answer
    
It's no worked on my Code :( Please help. Meybe in element it should be ? –  Grzegorz Z Dec 12 '12 at 13:20
1  
Want to go to chat? chat.stackexchange.com/rooms/6545/… –  Norbert Dec 12 '12 at 13:37
    
I'm on chat :) :) –  Grzegorz Z Dec 12 '12 at 13:42
1  
Ok, I am sorry, I made a stupid mistake. I have edited my response. –  Norbert Dec 12 '12 at 14:06
    
Now its a great solution :) –  Grzegorz Z Dec 12 '12 at 14:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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