I've created an example in which the data is in a List
. This custom class Person
is simply to capture you're original example above:
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
}
This method randomizes the order of names in a List<Person>
:
public string GetRandomNames(List<Person> people)
{
int numberOfPeople = people.Count;
string nameLabel;
string[] names = new string[numberOfPeople];
Random r = new Random();
for(int i = 0; i<numberOfPeople; i++)
{
int randomIndex = r.Next(0, people.Count);
names[i] = people[randomIndex].Name;
people.RemoveAt(randomIndex);
}
foreach(string name in randomNames)
{
nameLabel += name + ", ";
}
return nameLabel;
}
For the purposes of this Example, I've created the List as below. Of course, your list would come from some other source, such as a the SQL database you mentioned.
List<Person> people = new List<Person>();
people.Add(new Person() { ID = 1, Name = "Nik" });
people.Add(new Person() { ID = 2, Name = "Steve" });
people.Add(new Person() { ID = 3, Name = "John" });
people.Add(new Person() { ID = 4, Name = "Denny" });
people.Add(new Person() { ID = 5, Name = "Joe" });
people.Add(new Person() { ID = 6, Name = "Mike" });
people.Add(new Person() { ID = 7, Name = "Elena" });
people.Add(new Person() { ID = 8, Name = "Michel" });
Usage would then be something along the lines of:
string nameLabel = GetRandomNames(people);
Please let me know if this doesn't answer your question.