NHibernate Where Contains / OrderBy / Skip / Take / Select T
本文关键字:Take Select Skip OrderBy Where Contains NHibernate | 更新日期: 2023-09-27 18:00:02
我正试图让这个查询正常工作,但没有成功。我有一个person对象,它有名字、姓氏、id和其他不应该返回的字段。查询应该只返回全名包含某些字符串值的人员。该查询也用于分页,所以我必须跳过记录并获取记录。
Session.QueryOver<Person>()
// Only fetch records where full name matches some string (Not working)
.WhereRestrictionOn(person => person.firstname + " " + person.lastname)
.IsInsensitiveLike("%bob%")
// Order by last then first name (Works if removing non-working parts)
.OrderBy(person => person.lastname)
.Asc
.ThenBy(person => person.firstname)
.Asc
// Select to different object (Not working)
.Select(person => new PersonDTO()
{
ID = person.ID,
Name = person.firstname + " " + person.lastname
})
// Skip and take (Works if removing non-working parts)
.Skip(50)
.Take(50)
.ToList();
var comboItem = new ComboBoxItem();
var result = Session.QueryOver<Person>()
.WhereRestrictionOn(person => Projections.Concat(person.firstname, " ", person.lastname))
.IsInsensitiveLike("%bob%")
.OrderBy(person => person.lastname)
.Asc
.ThenBy(person => person.firstname)
.Asc
.SelectList(list => list
.Select(person => person.ID).WithAlias(() => comboItem.id)
.Select(person => Projections.Concat(person.firstname, " ", person.lastname)).WithAlias(() => comboItem.text)
)
.TransformUsing(Transformers.AliasToBean<ComboBoxItem>())
.Skip(50)
.Take(50)
.List<ComboBoxItem>()
.ToList();