Returning Anonymous types
As of v9.1 you can deserialize to anonymous types via ToEnumerableOf, ToArrayOf and ToListOf
. You do this by passing in an anonymous type template which is used when deserializing.
As of v11.5 you can use the type being queried to create/map the projection. This is the recommended way, since it will be refactoring friendly.
db.UseOnceTo()
.Query<Customer>(c => c.Name = "Daniel")
.ToArrayOf(c => new { c.CustomerNo, c.BonusPoints });
You could of course pass in a template and not use the queried type to make the projection.
db.UseOnceTo()
.Query<Customer>(c => c.Name = "Daniel")
.ToArrayOf(new { CustomerNo = "", BonusPoints = default(int) });
Please note! Both API's requires the Anonymous object to have same member definitions. Lets take the example above and change the projection to use another name for BonusPoints
. We will use ThePoints
.
db.UseOnceTo()
.Query<Customer>(c => c.Name = "Daniel")
.ToArrayOf(c => new { c.CustomerNo, ThePoints = c.BonusPoints });
This will not cause an exception, but it will also NOT GIVE YOU ANY VALUE for ThePoints
. Why? Well, the structure in SisoDb is stored as JSON, and the deserialization has no way knowing that you want to remap a member. Hence it will not populate that member.