How to get a Random Object using Linq

前端 未结 9 1484
心在旅途
心在旅途 2020-12-29 02:34

I am trying to get a random object within linq. Here is how I did.

//get all the answers
var Answers = q.Skip(1).Take(int.MaxValue);
//get the random number         


        
9条回答
  •  鱼传尺愫
    2020-12-29 03:28

    I have product table in database ,every time user enters one product detail I want to show 10 similar products in below of page.And in every refresh this list must be change .it must come randomly.

    Linq looks like this

    var products =
                DataContextFactory.GetDataContext()
                    .Set()
                    .Where(x =>x.Id!=id)
                    .OrderBy(emp => Guid.NewGuid())
                    .Take(10).ToList();
    
    x.Id!=id 
    

    this only for not put selected product to list .

    It works perfect

提交回复
热议问题