get random data with entity framework

Cihangir 1 Reputation point
2022-06-25T15:12:49.743+00:00

How can i get random data from database with entity framework, *vers. 6

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,371 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,244 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2022-06-25T19:27:39.923+00:00

    Without specifics, here is how to get random records from a model.

    public static List<Products> RandomProducts(int count)  
    {  
        using var context = new Context();  
          
        Random rand = new Random();  
        int skipper = rand.Next(0, context.Products.Count());  
      
        return context  
            .Products  
            .OrderBy(product => Guid.NewGuid())  
            .Skip(skipper)  
            .Take(count)  
            .ToList();  
    }  
    

    Usage

    var products = CoreOperations.RandomProducts(7);  
    foreach (var product in products)  
    {  
        Console.WriteLine($"{product.ProductId,-4}{product.ProductName}");  
    }  
      
    

    215005-f1.png

    Edit, here is a generic method

    /// <summary>  
    /// Get list of random records for <see cref="TModel"/> by <see cref="count"/>  
    /// </summary>  
    /// <typeparam name="TModel">Model to read</typeparam>  
    /// <param name="count">Max records</param>  
    /// <returns>List of <see cref="TModel"/></returns>  
    /// <remarks>  
    /// Not guaranteed to return <see cref="count"/> but will return records  
    /// </remarks>  
    public static List<TModel> Random<TModel>(int count) where TModel : class  
    {  
        using var context = new Context();  
      
        Random rand = new Random();  
        int skipper = rand.Next(0, context.Set<TModel>().Count());  
        return context.Set<TModel>().ToList()  
            .OrderBy(x => Guid.NewGuid())  
            .Skip(skipper)  
            .Take(count).ToList();  
    }  
    

    Usage

    List<Products> products = CoreOperations.Random<Products>(7);  
    foreach (var product in products)  
    {  
        Console.WriteLine($"{product.ProductId,-4}{product.ProductName}");  
    }  
      
    Console.WriteLine();  
      
    List<Categories> categories = CoreOperations.Random<Categories>(3);  
    foreach (var product in categories)  
    {  
        Console.WriteLine($"{product.CategoryId,-4}{product.CategoryName}");  
    }  
    

    214979-f2.png

    0 comments No comments

  2. Rijwan Ansari 746 Reputation points MVP
    2022-06-26T13:45:30.567+00:00

    Hi,

    Initially, you need to get the random number from 1 to max record as shown:

    Random rand = new Random();  
    int toSkip = rand.Next(1, db.MyTables.Count);  
    

    Then you can use order by you can use the Guid.NewGuid() as shown in below sample.

     db.MyTables.OrderBy(x=>x.Guid.NewGuid()).Skip(toSkip).Take(5).ToList()  
    

    That's it.