Random row from Linq to Sql

前端 未结 15 2312
南笙
南笙 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:11

    Another sample for Entity Framework:

    var customers = db.Customers
                      .Where(c => c.IsActive)
                      .OrderBy(c => Guid.NewGuid())
                      .FirstOrDefault();
    

    This does not work with LINQ to SQL. The OrderBy is simply being dropped.

提交回复
热议问题