2

I have a simple model

public class OneModel
   {
      public string Email { get; set; }

      public string Name { get; set; }
   }

And I created a simple list (OneModel datatype):

List<OneModel> someLists = new List<OneModel>{
            new OneModel{Name="A 1", Email="[email protected]"},
            new OneModel{Name="A 2", Email="[email protected]"},
            new OneModel{Name="A 3", Email="[email protected]"},
            new OneModel{Name="B 1", Email="[email protected]"},
            new OneModel{Name="C 1", Email="[email protected]"},
  }

I want to create another list with random concatenated Name from first list, like as: List<string> ... {"A 1-A 3", "A 2-C 1",...} or something like that.

How to do that ?

1 Answer 1

3

Assuming .NET Framework 4.0:

var random = new Random();
var result = new List<string>();
var count = random.Next(10, 20); // How many results do you want, really!?
for (int i = 0; i < count; ++i) {
    var query = from item in someLists
                order by random.Next()
                select item.Name;
    var items = query.Take(2);   // I assume you want two "names" in each item
    result.Add(string.Join("-", items));
}

Examine result for your random-length list of random randomness...

But what do you need it for...?

Sign up to request clarification or add additional context in comments.

1 Comment

I want to learn more about list and get random values from generic list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.