NHibernate query order random

I am trying to write a query that returns randomly sorted results. I found this article Linq Orderby random ThreadSafe for use in ASP.NET gave me some basic clues how to do this. But I get the following exception:

The variable’x’ of type’Accomodations.DAL.Model.Generated.Accomodation’ referenced from scope”, but it is not defined

This is my query:

var query = session.QueryOver()
.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();

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())
.Skip(skip)
.Take(searchParams.PageSize)
.Future ();

searchResults = futurePage.ToList();
numberOfResults = futureCount.Value;
});

Any suggestions would be greatly appreciated. Thank you

This is a good example of how to do this. This is the one I’m currently using Kind of technology.

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

Edit

p>

The following is taken from the above article, I modified it slightly to include Skip.

public IList GetRandomTestimonials(int count, int skip ) {
return Session
.QueryOver()
.OrderByRand om()
.Take(count)
.Skip(skip)
.List();
}

I’m trying to write a query that returns randomly sorted results. I found that this article Linq Orderby random ThreadSafe for use in ASP.NET gave me some basic clues how to do this. But I get the following exception:

The variable’x’ of type’Accomodations.DAL.Model.Generated.Accomodation’ referenced from scope”, but it is not defined

This is my query:

< p>

var query = session.QueryOver()
.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);
< br /> if (searchParams.Location != 0)
query = query.Where(x => x.Location.Id == searchParams.Location);

var futureCount = query.Clone().Select(Projections.RowCount()).FutureValue();

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())
.Skip(skip)
.Take(searchParams.PageSize)
.Future();

searchResults = futurePage.ToList();
numberOfResults = futureCount.Value;
});

Any suggestions would be greatly appreciated. Thank you

This is a good example of how to do this. This is A technique I am currently using.

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

Edit

The following is taken from the article above, I modified it slightly to include Skip.

public IList GetRandomTestimonials (int count, int skip) {
return Session
.QueryOver()
.Order ByRandom()
.Take(count)
.Skip(skip)
.List();
}

Leave a Comment

Your email address will not be published.