Random row from Linq to Sql

前端 未结 15 2520
南笙
南笙 2020-11-22 05:39

What is the best (and fastest) way to retrieve a random row using Linq to SQL when I have a condition, e.g. some field must be true?

15条回答
  •  自闭症患者
    2020-11-22 06:15

    List lst = new List();
    lst.Add("Apple"); 
    lst.Add("Guva");
    lst.Add("Graps"); 
    lst.Add("PineApple");
    lst.Add("Orange"); 
    lst.Add("Mango");
    
    var customers = lst.OrderBy(c => Guid.NewGuid()).FirstOrDefault();
    

    Explanation: By inserting the guid (which is random) the order with orderby would be random.

提交回复
热议问题