2016-08-03 6 views
0

私は運がうまく動作するようにこのクエリを取得しようとしています。私は、返されるべきではない名、姓、ID、その他のフィールドを持つ人物オブジェクトを持っています。クエリは、フルネームに文字列値が含まれている人のみを返します。クエリはページングにも使用されているので、レコードをスキップしてレコードを取得する必要があります。NHibernate Where Contains/OrderBy/Skip/Take/New Entityへの選択

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

どのように「好き」は機能していませんか?レコードが多すぎますか?少なすぎる? "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

関連する問題