Get a random row with LINQtoSQL

后端 未结 6 1572
忘掉有多难
忘掉有多难 2021-01-20 04:39

Is there a way to return a random row from a table using LINQToSQL?

6条回答
  •  心在旅途
    2021-01-20 05:19

    Never tried it, but i suspect the following will get the job done

    Random rnd = new Random();
    
    int count = db.MyTable.Count();
    int skip = rnd.Next(count);
    
    var row = (from r in db.MyTable
               select r).Skip(skip).Take(1);
    

提交回复
热议问题