NHibernate 快速入门(四)使用 HQL 查询数据

时间:2022-02-20 08:22:35

1、from 子句

var db = new NorthwindDataContext();
var categories
= db.Session.CreateQuery("from Category")
.List
<Category>();
foreach (var category in categories)
{
Console.WriteLine(category.CategoryName);
}

 

2、select 子句

var db = new NorthwindDataContext();
var categories = db.Session.CreateQuery("from Category as c select c.CategoryID, c.CategoryName")
.List
<Category>();
foreach (var category in categories)
{
Console.WriteLine(category.CategoryName);
}

 

3、where 子句

var db = new NorthwindDataContext();
var categories
= db.Session.CreateQuery(@"from Category as c
where c.CategoryID > 1
select c.CategoryID, c.CategoryName
")
.List
<Category>();
foreach (var category in categories)
{
Console.WriteLine(category.CategoryName);
}