简体   繁体   中英

Dynamic DTO from nhibernate projections criteria

I have a projection List in my c# code :

var criteria = DetachedCriteria.For(this.modelType);

         for (int i = 0; i < this.fields.Length; i++)
            projections.Add(Projections.Property(this.fields[i]));

        if (fields.Length > 0)
            criteria.SetProjection(projections);

if I don´t set any field in my projection my return is follow:

在此处输入图片说明

but if I set projections in my code, my result is follow:

在此处输入图片说明

How can I convert the list with projections in a Dynamic DTO object?

What we need is transformer:

criteria
    .SetResultTransformer(
      NHibernate.Transform.Transformers.AliasToBean<MyEntity>())

or without generic

criteria
    .SetResultTransformer(
      NHibernate.Transform.Transformers.AliasToBean(this.modelType))

The point with transformers is to use aliasing (see the .As() ):

.SetProjection(Projections.ProjectionList()
  .Add(Projections.Property("Property1").As("Property1"))
  .Add(Projections.Property("Property2").As("Property2"))
  .Add(Projections.Property("Property3").As("Property3"))
)

Check more here eg:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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