Random Nhibernate Request Order

I am trying to write a query that returns randomly ordered results. I found this Linq Orderby random ThreadSafe post for use in ASP.NET , which gave me some basic tips on how to do this. But I get the following exception:

variable 'x' of type 'Accomodations.DAL.Model.Generated.Accomodation' referring to scope '' but not defined

Here is my request:

var query = session.QueryOver<Accomodation>() .OrderBy(x => (~(x.Id & seed)) & (x.Id | seed)).Asc; // this is the problematic line of code if (searchParams.District != 0) query = query.Where(x => x.District.Id == searchParams.District); if (searchParams.Region != 0) query = query.Where(x => x.Region.Id == searchParams.Region); if (searchParams.Location != 0) query = query.Where(x => x.Location.Id == searchParams.Location); var futureCount = query.Clone().Select(Projections.RowCount()).FutureValue<int>(); SearchAccomodationResultItem resultItemAlias = null; var futurePage = query .SelectList(list => list .Select(x => x.Id).WithAlias(() => resultItemAlias.Id) .Select(x => x.AccomodationType.Id).WithAlias(() => resultItemAlias.AccomodationTypeId) .Select(x => x.Region.Id).WithAlias(() => resultItemAlias.RegionId) .Select(x => x.Name).WithAlias(() => resultItemAlias.Title) .Select(x => x.MaxCapacity).WithAlias(() => resultItemAlias.MaxCapacity) .Select(x => x.MinPrice).WithAlias(() => resultItemAlias.MinPrice) .Select(x => x.MinStayLength).WithAlias(() => resultItemAlias.MinStayLength) .Select(x => x.MainImageName).WithAlias(() => resultItemAlias.ImgSrc) ) .TransformUsing(Transformers.AliasToBean<SearchAccomodationResultItem>()) .Skip(skip) .Take(searchParams.PageSize) .Future<SearchAccomodationResultItem>(); searchResults = futurePage.ToList(); numberOfResults = futureCount.Value; }); 

Any suggestion will be appreciated. Thanks

+4
source share
2 answers

Here is a good example of how to do this. This is the method that I am currently using.

http://puredotnetcoder.blogspot.com/2011/09/nhibernate-queryover-and-newid-or-rand.html

Edit

Below is the article above, and I modified it a bit by adding Skip .

 public IList<CmsTestimonial> GetRandomTestimonials(int count, int skip) { return Session .QueryOver<CmsTestimonial>() .OrderByRandom() .Take(count) .Skip(skip) .List(); } 
+1
source

To use the above approach with a search call, you can save the seed for the user (perhaps for each session), and then use the RAND (seed) function in SQL - because you use the same seed, it will generate the same a sequence of pseudo random numbers and thus allow paging

+1
source

Source: https://habr.com/ru/post/1413324/


All Articles