2016-08-03 49 views
0

我試圖讓這個查詢沒有運氣的工作。我有一個人物對象,名字,姓氏,ID和其他不應該被返回的字段。該查詢應該只返回其完整名稱包含一些字符串值的人員。該查詢也用於分頁,所以我必須跳過記錄並記錄。NHibernate哪裏包含/ OrderBy /跳過/採取/選擇新實體

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

@stuartd的地方/像不工作而選擇不工作。 – Jeff

+0

「like」不工作的方式是什麼?記錄太多?太少了?如果「bob」是名字或姓氏的一部分,你會得到任何結果嗎? –

+0

@GrantWinney System.InvalidOperationException:從範圍''引用類型'Person'的變量'person',但沒有定義 – Jeff

回答

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

我建議使用:IsInsensitiveLike(「bob」,MatchMode.Anywhere)'以獲取更多可讀代碼。 – Najera