1
爲了簡化NHibernate的限制一樣,我們想有兩個實體:嵌套字符串屬性
public class Entity
{
public string Value { get; set; }
public ChildEntity Child { get; set; }
}
public class ChildEntity
{
public string Value { get; set; }
}
我需要找到任何Value
或Child.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
不能正確處理別名。
,我曾嘗試另一種方法是.JoinQueryOver()
這是由this post建議:
return session
.QueryOver<Entity>()
.Where(Restrictions.InsensitiveLike("Value", query))
.JoinQueryOver(e => e.Child)
.Where(Restrictions.InsensitiveLike("Value", query));
這個事情的作品,除了一兩件事:它返回其中兩個Value
和Child.Value
就像query
所有項目。我需要相同的東西,但邏輯爲or
。
應該怎麼做才能使它工作?我想使用.QueryOver()
,無論是否帶有別名,但沒有.CreateCriteria()
,但是如果您能幫助我解決任何工作問題,我們將不勝感激。
您是否添加了MatchMode選項? –
@WillyDavidJr你能解釋一下你的意思嗎? MatchMode隻影響字符串的比較方式,不是嗎?沒有更多的相關代碼比這個最小的例子。 –