2017-08-03 7 views
1

は、の2つのエンティティがあります推測してみましょう:のNHibernateのように制限簡略化のため

public class Entity 
{ 
    public string Value { get; set; } 

    public ChildEntity Child { get; set; } 
} 

public class ChildEntity 
{ 
    public string Value { get; set; } 
} 

私はValueChild.Valueいずれかが指定された文字列queryのような鈍感であるすべてのエンティティを見つける必要があります。私は今では持っているものだ

は:

Entity entity = null; 
ChildEntity child = null; 

var nhibernateQuery = session 
    .QueryOver(() => entity) 
    .JoinAlias(() => entity.Child,() => child); 

if (!string.IsNullOrWhiteSpace(query)) 
{ 
    nhibernateQuery = nhibernateQuery 
     .Where(
      Restrictions.Or(
       Restrictions.On(() => entity).IsInsensitiveLike(query), 
       Restrictions.On(() => child).IsInsensitiveLike(query) 
      ) 
     ); 
} 

return nhibernateQuery.List().ToArray(); 

私はNullReferenceException取得 - 正しく、エイリアスを処理しないRestrictions.Onのように思えます。私が試してみました

別のアプローチは、this postによって提案され.JoinQueryOver()です:

return session 
    .QueryOver<Entity>() 
     .Where(Restrictions.InsensitiveLike("Value", query)) 
    .JoinQueryOver(e => e.Child) 
     .Where(Restrictions.InsensitiveLike("Value", query)); 

このことは、一つのことを除いて動作します:それは両方ValueChild.Valuequeryのようなもので、すべてのアイテムを返します。私は同じことが必要ですが、orロジックです。

これを機能させるには何が必要ですか?エイリアスの有無にかかわらず、.CreateCriteria()を使用しないで.QueryOver()を使用したいと思いますが、実用的な解決策を教えていただければ幸いです。

+0

MatchModeオプションを追加しましたか? –

+0

@WillyDavidJrあなたは何を意味するのか説明していただけますか? 'MatchMode'は文字列の比較方法に影響しますか?この最小限の例よりも関連するコードはありません。 –

答えて

0

NHibernate LINQ .Query<>()を使用することによって問題が解決されました。
すべての結合とリレーションを単独で解決できます。
同時に、.Contains()メソッドは、私に合ったMS SQL文の大文字と小文字を区別しないLIKE文に変換されます。

var nhibernateQuery = session 
    .Query<Entity>(); 

if (!string.IsNullOrWhiteSpace(query)) 
{ 
    nhibernateQuery = nhibernateQuery 
     .Where(e => e.Value.Contains(query) || e.Child.Value.Contains(query)); 
} 

return nhibernateQuery.ToArray();