NHibernate: HasMany components and Where/Contains clause
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.
精彩评论