简体   繁体   English

流利的NHibernate-集合不过滤

[英]Fluent NHibernate - Collections not filtering

I have a session criteria statement (Fluent NHibernate) that does not seem to filter the child collection even though I have Expressions/Restrictions defined. 我有一个会话条件语句(Fluent NHibernate),即使我定义了表达式/限制,它似乎也不能过滤子集合。

ICriteria criteria = session.CreateCriteria(typeof(MyClass));
criteria.CreateAlias("MyCollection", "MC");
criteria.Add(Restriction.Eq("MC.Property", value));
IList<MyClass> list = criteria.List<MyClass>();

This returns all the objects of type MyClass that have MyCollection.Property = value , however MyCollection is not filtered to MyCollection.Property = value 这将返回具有MyCollection.Property = value所有MyClass类型的对象,但是MyCollection不会筛选为MyCollection.Property = value

It seems like only the Root objects get filtered. 似乎只有Root对象被过滤了。

Thanks. 谢谢。

That's correct, it will only filter the root entity. 没错,它只会过滤根实体。 If the query changed the items in the collection of the root entity you could have a terrible problem: if you save the entity again, items that were filtered off of the collection will be removed of the realtionship permanently! 如果查询更改了根实体集合中的项目,那么您可能会遇到一个可怕的问题:如果再次保存该实体,那么从集合中过滤掉的项目将永久从Realtionship中删除! And sure nobody wants that. 并且确保没有人想要那个。

If you want that behaviour you'll have to do it manually (via foreach after the entities were loaded), although I wouldn't recomend that for the reason above. 如果您想要这种行为,则必须手动执行(通过在加载实体后通过foreach进行),尽管出于上述原因我不建议这样做。 My sugestion is to make the entity in the collection the root of the query. 我的建议是使集合中的实体成为查询的根。

I've only found a few dodgy links about doing that - so this is at your own risks :). 我只发现了一些与此有关的狡猾链接-因此,后果自负:)。

It seems it should work if you add the following: 如果添加以下内容,则似乎应该可以使用:

criteria.CreateCriteria("MC", JoinType.LeftOuterJoin);

I wouldn't recommend it though as per Pedro's answer. 尽管按照佩德罗的回答,我不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM