1

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:

enter image description here

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

enter image description here

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

Igor Monteiro
  • 933
  • 1
  • 11
  • 29

1 Answers1

1

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 e.g.:

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • I need a dynamic DTO object .. the user chooses which fields are designed I return one dynamic list him – Igor Monteiro Nov 18 '15 at 17:48
  • I'd say, that term dynamic DTO does not make sense. We either have DTO defined in C# as a standard class, or it is a dictionary. If this is a dictionary, the resulting `IList` should be easy to convert into IDictionary. But still you need to know the keys... (like alias above) ... [here](http://stackoverflow.com/a/13632872/1679310) few words about dto and poco – Radim Köhler Nov 18 '15 at 17:50
  • The second example help me.. tks – Igor Monteiro Nov 18 '15 at 17:51
  • That is great to see, really. Enjoy mighty NHibernate Igor ;) – Radim Köhler Nov 18 '15 at 17:51