1

I have query:

 var query = this.session.QueryOver<Products>()

.Where(uic => uic.PageNumber == nextPage[0])

.SingleOrDefault(uic => uic.ProductNumber)

But this query result is type Products. It is possible that result will be only integer type of column ProductNumber ?

2 Answers 2

4

Try something like this:

var query = this.session.QueryOver<Products>()
    .Where(uic => uic.PageNumber == nextPage[0])
    .Select(uic => uic.ProductNumber)
    .SingleOrDefault<int>();

Since you need a single primitive type value, you can do .Select to define the result column, and then do .SingleOrDefault to get the only result. For complex types, you'd need to use transformers.

You can find more info about QueryOver in this blog post on nhibernate.info: http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html

Sign up to request clarification or add additional context in comments.

3 Comments

I think you want SingleOrDefault here and not Single.
Yes, SingleOrDefault should be used if "null result" is possible - 0 will be the default. If you always expect one item, then Single should be OK.
I don't think Single can even be called here though (i.e.: this won't compile). SingleOrDefault is a method on IQueryOver<TRoot, TSubtype>, while Single is not.
2

You can use Miroslav's answer for QueryOver, but this would look cleaner with LINQ:

var productNumber = session.Query<Products>()
                           .Where(uic => uic.PageNumber == nextPage[0])
                           .Select(uic => uic.ProductNumber)
                           .SingleOrDefault();

Notice you don't need a cast, as the Select operator changes the expression type to the return type of its parameter (which is the type of ProductNumber).

1 Comment

This LINQ query also will be invoke at db side?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.