C# NHibernate别名ToBean变压器协会

C# NHibernate别名ToBean变压器协会,c#,nhibernate,nhibernate-criteria,C#,Nhibernate,Nhibernate Criteria,我正在尝试使用以下语句获取包含我要查找的字段的实体: retVal = session.CreateCriteria(typeof(MyEntity)) .CreateAlias("MyEntityProperty", "MyEntityProperty") .Add(Restrictions.Eq("MyEntityProperty.Year", year)) .SetProjection(

我正在尝试使用以下语句获取包含我要查找的字段的实体:

retVal = session.CreateCriteria(typeof(MyEntity))
            .CreateAlias("MyEntityProperty", "MyEntityProperty")
            .Add(Restrictions.Eq("MyEntityProperty.Year", year))
            .SetProjection(
                Projections.Distinct(
                    Projections.ProjectionList()
                        .Add(Projections.Property("Property1"), "Property1")
                        .Add(Projections.Property("Property2"), "Property2")
                        .Add(Projections.Property("MyEntityProperty.RegisteredUser"), "MyEntityProperty.RegisteredUser")
                        .Add(Projections.Property("MyEntityProperty.CompanyInfo"), "MyEntityProperty.CompanyInfo")
                                                )
            )
            .SetResultTransformer(Transformers.AliasToBean(typeof(MyEntity)))
            .List<MyEntity>()
            .Cast<BaseMyEntity>();
retVal=session.CreateCriteria(typeof(MyEntity))
.CreateAlias(“MyEntityProperty”、“MyEntityProperty”)
.Add(Restrictions.Eq(“MyEntityProperty.Year”,Year))
.SetProjection(
投影,清晰(
投影。投影列表()
.添加(项目.财产(“财产1”),“财产1”)
.添加(项目.财产(“财产2”),“财产2”)
.Add(Projections.Property(“MyEntityProperty.RegisteredUser”),“MyEntityProperty.RegisteredUser”)
.Add(Projections.Property(“MyEntityProperty.CompanyInfo”),“MyEntityProperty.CompanyInfo”)
)
)
.SetResultTransformer(Transformers.AliasToBean(typeof(MyEntity)))
.List()
.Cast();
MyEntity是我要返回的实体,MyEntityProperty是MyEntity的属性,MyEntityProperty是另一个实体(MyEntityProperty类型)

我得到的错误是
在类“MyEntity”中找不到属性“MyEntityProperty.RegisteredUser”的setter

AliasToBean转换器是否无法处理子实体?或者我还需要做些什么才能让它发挥作用呢?

这是我的杰作。。。我用它来变换任何层次的投影深度。拿着它,像这样使用它:

.SetResultTransformer(new DeepTransformer<MyEntity>())

我不是把事情弄得太复杂了吗

我所需要做的不是在子实体上设置字段,而是引用实体字段本身:

.Add(Projections.Property("MyEntityProperty"), "MyEntityProperty")
尼伯内特把它填得很好


但我很高兴我问了,因为我得到了Radim非常有用的代码:-)

我实际上已经知道我把事情弄得比我需要的更复杂了,但是非常感谢你的代码,它看起来真的很有用。dictionary.Is()这像是一种“生存还是毁灭”的东西吗?还是某种类型的空检查?@user99999928正确。。这只是一个扩展,正在检查null。。。(通过任何类型提供它,包括ValueTypes——语法流畅的抽象)@RadimKöhler。扩展来自哪里:
.IsNull()
.Is()
?他们是干什么的?@FernandoLeal。。。一种原则(阅读完整的代码)。。方便的
公共静态bool Is(此对象值){return value!=null;}
具有优势-它可以在其他语言中重用(例如Typescript等)。如果要设置子字段,该怎么办?仅供参考:
.Add(Projections.Property("MyEntityProperty"), "MyEntityProperty")