开发者

NHibernate: HasMany components and Where/Contains clause

开发者 https://www.devze.com 2022-12-08 02:12 出处:网络
I\'m trying to work out how to create a query using Linq to NHibernate. I have two classes like this: public class Foo

I'm trying to work out how to create a query using Linq to NHibernate.

I have two classes like this:

public class Foo
{
    private ISet<Bar> _bars = new HashedSet<Bar>();
    public virtual ISet<Bar> Bars
    {
        get { return _bars; }
        set { _bars = value; }
    }
}

public class Bar
{
    public string Name { get; set; }
    public string Color { get; set; }
}

Foo's Bar collection is mapped as a one-to-many component collection.

Now I want to run a query that should look something like this:

var myBar = new Bar { Name = "test", Color = "testColor"开发者_C百科 };

var matchingFoos = Session.Linq<Foo>
                   .Where(foo => foo.Bars.Contains(myBar),
                          new BarEqualityComparer())
                   .ToList();

I am not sure if this is correct, but whenever I run this query I get a NullReferenceException from inside a method called NHibernate.Linq.Visitors.WhereArgumentsVisitor.GetCollectionContainsCriteria method.

Could anyone help me out with an alternative means of running this query?


The BarEqualityComparer will be the point of failure for sure. There is no simple way for the provider to translate custom class to an SQL statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消