S
shapper
Hello,
I am using the following extension to get one random item from an
IEnumerable<T>:
public static T Random<T>(this IEnumerable<T> collection) {
if (collection.Count<T>() == 0) return default(T);
Int32 position = random.Next(collection.Count<T>());
return collection.ElementAt(position);
} // Random
Is it possible, and does it make sense (I think it does), to transform
my extension to be able to use it as follows:
.... collection.Random(p => p.Country == "US");
So I would like to get one random element but only from the ones which
country is equal to US.
Another extra option would be select how many random records to be
returned (if not one) but in this case I am not sure if this is
possible.
Thank you,
Miguel
I am using the following extension to get one random item from an
IEnumerable<T>:
public static T Random<T>(this IEnumerable<T> collection) {
if (collection.Count<T>() == 0) return default(T);
Int32 position = random.Next(collection.Count<T>());
return collection.ElementAt(position);
} // Random
Is it possible, and does it make sense (I think it does), to transform
my extension to be able to use it as follows:
.... collection.Random(p => p.Country == "US");
So I would like to get one random element but only from the ones which
country is equal to US.
Another extra option would be select how many random records to be
returned (if not one) but in this case I am not sure if this is
possible.
Thank you,
Miguel