Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. public class LocationDataAccess : ILocationDataAccess
  2.     {
  3.         private IDBConnectionFactory Conn { get; }
  4.  
  5.         public LocationDataAccess(IDBConnectionFactory connection)
  6.         {
  7.             Conn = connection;
  8.         }
  9.  
  10.  
  11.         public async Task<IEnumerable<Location>> GetAllLocations()
  12.         {
  13.             var sql = @"Select * FROM Location";
  14.             using (var connection = await Conn.GetOpenConnectionAsync())
  15.             {
  16.                 var locations = await connection.QueryAsync<Location>(sql);
  17.  
  18.                 return locations.ToList();
  19.             }
  20.         }
  21.  
  22.         public async Task<Location> Randomize()
  23.         {
  24.             var locations = await GetAllLocations();
  25.             Random rand = new Random();
  26.             int toSkip = rand.Next(0, locations.Count());
  27.  
  28.             var loc =  locations.OrderBy(x => x.ID)
  29.                                .Skip(toSkip)
  30.                                .Take(1).First();
  31.  
  32.             return loc;
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement