按照任何返回的类或者组件的属性进行分组
例如按照标题进行分组然后统计标题重复的次数
public IList<object[]> Groupby()
{
return session.CreateQuery("select n.Title, count(c.Title) from News n group by n.Title").List<object[]>();
}
Code language: PHP (php)
按照标题查询
public IList<News> GetNewsByTitle(string title)
{
//写法 1:拼接字符串 (可能会引起SQL注入)
return session.CreateQuery("from News n where n.Title = '" + title + "'")
.List<News>();
}
public IList<News> GetNewsByTitle2(string title)
{
//写法 2:位置型参数 容易搞错
return session.CreateQuery("from News n where n.Title =? ")
.SetString(0, title)
.List<News>();
}
public IList<News> GetNewsByTitle3(string title)
{
//写法 3:命名型参数(推荐)
return session.CreateQuery("from News n where n.Title =:fn").SetString("fn", title).List<News>();
}
Code language: PHP (php)
除了SetString 还有
SetInt32(“id”, id)
Previous: where和order by使用
Next: Nhibernate的条件查询ICriteria