构造多个查询。效果不错。注意Nhibernate CreateMultiQuery()这个方法
public void MultiQueryTest(int var)
{
ISession session = null;
session = Class1.GetFactory().OpenSession();
IList result = session.CreateMultiQuery().Add("from Archives where id>:id")
.Add("select count(*) from Archives a where id>:id").SetInt32("id", var).List();
//获得第一个查询结果
IList archives = (IList)result[0];
long count = (long)((IList)result[1])[0];
//当然也可以session.CreateQuery 构造查询
//IList result = session.CreateMultiQuery()
// .Add(session.CreateQuery("from Archives where id>:id").SetFirstResult(5).SetMaxResults(10))
// .Add(session.CreateQuery("select count(*) from Archives a where id>:id"))
// .SetCacheable(true).SetInt32("id", var).List();
// IList archives = (IList)result[0];
//long count = (long)((IList)result[1])[0];
}
//helper
public class Class1
{
private static ISessionFactory factory;
private static string _mappingAssembly;
static Class1()
{
_mappingAssembly = ConfigurationManager.AppSettings["EntityAssemblyName"];
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
foreach (string assembly in _mappingAssembly.Split(','))
{
try
{
cfg.AddAssembly(assembly);
}
catch (MappingException me)
{
throw new MappingException("Assembley Could Not Be Loaded: " + assembly, me);
}
}
factory = cfg.BuildSessionFactory();
}
public static ISessionFactory GetFactory()
{
return factory;
}
}