S
shapper
Hello,
I have the following extension to get a random item from a
IEnumerable.
private static Random random = new Random();
public static T Random<T>(this IEnumerable<T> target) {
Int32 position = random.Next(target.Count<T>());
return target.ElementAt(position);
} // Random
How can I "do nothing" if the target is empty?
This can happen for example when I filter a list of records using a
condition and no items satisfy that condition:
Product product = products.Where(p => p.Price > 1000).Random()
Thanks,
Miguel
I have the following extension to get a random item from a
IEnumerable.
private static Random random = new Random();
public static T Random<T>(this IEnumerable<T> target) {
Int32 position = random.Next(target.Count<T>());
return target.ElementAt(position);
} // Random
How can I "do nothing" if the target is empty?
This can happen for example when I filter a list of records using a
condition and no items satisfy that condition:
Product product = products.Where(p => p.Price > 1000).Random()
Thanks,
Miguel