如何用nhibernate使用contains查询相关对象

时间:2012-07-17 23:02:55

标签: c# linq nhibernate

我对NHibernate很新,所以这个应该很简单:

    public IList<Ad> Search(string query)
    {
        return unitOfWork.Session
            .QueryOver<Ad>()
            .JoinQueryOver<AdProperty>(x => x.Properties)
                .Where(ad => ad.Value.Contains(query))
            .List();
    }

我要做的当然是搜索AdProperty包含特定字符串的广告,然后传回相应的广告对象。 (我意识到这不会产生最佳查询,但现在这已经足够了)

问题

我无法使用.Contains,因为它无法识别。那么我如何使用NHibernate正确地做到这一点?

我看过NHibernate query looking for the related object's related object,但我无法让它发挥作用。

注意

我正在使用NHibernate 3.0 +

1 个答案:

答案 0 :(得分:1)

经过一些更多的摆弄,我得到了上面的例子(如上所述)。我将留下我的解决方案供将来参考。

    public IList<Ad> Search(string query)
    {
        return unitOfWork.Session
            .CreateCriteria<Ad>()
            .CreateAlias("Properties", "props")
            .Add(Expression.InsensitiveLike("props.Value", query, MatchMode.Anywhere))
            .List<Ad>();
    }

希望它可以帮助某人: - )