In some cases it’s a lot easier to just write a simple SQL statement instead of using LINQ, HQL or one of the other syntaxes that NHibernate supports.
Executing a raw SQL:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var query = "SELECT TOP 10000 o.* " | |
+ " from ORDERS o where o.Year in (:orderYear));"; | |
var session = sessionFactory.OpenSession(); | |
var result =session.CreateSQLQuery(query) | |
.SetInt32("orderYear",2012) | |
.List(); |
Converting the result to a typed object:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var query = "SELECT TOP 10000 o.* " | |
+ " from ORDERS o where o.Year in (:orderYear));"; | |
var session = sessionFactory.OpenSession(); | |
var result =session.CreateSQLQuery(query) | |
.AddEntity(typeof(Order)) | |
.SetInt32("orderYear",2012) | |
.List<Order>(); |