Get Random Entity
async static Task<T> GetRandomEntity<T>(IQueryable<T> query, CancellationToken token)
{
var max = query.Count();
return await query.Select(x => x)
.Skip(new Random().Next(0, max - 1))
.Take(1)
.FirstAsync(token);
}
Usage
var offense = await GetRandomEntity(_context.Offenses, token);
Comments
0 responses