NHibernate Where Contains / OrderBy / Skip / Take / Select To New Entity

时间:2016-08-03 22:55:35

标签: c# asp.net .net nhibernate

我试图让这个查询没有运气。我有一个person对象,其中包含名字,姓氏,id和其他不应返回的字段。查询应仅返回其FULL NAME包含某些字符串值的人员。该查询也用于分页,因此我必须跳过记录并记录。

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();

1 个答案:

答案 0 :(得分:1)

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();