Convert Anonymous to Named Type (C# only): my feedback

the named type should use only automatic property and no constructor. otherwise linq2sql can have a different behaveour (different Sql execution plan)

2 comments


Hi.

I see your suggestion.

Compiler actually generates READONLY properties with backing fields. And NO default constructor is generated.

Generation of auto-properties may be useful for creating of ‘data classes’ from scratch, e.g. when you intentionally create anonymous object creation expression to generate a full-blown class.

0

> Compiler actually generates READONLY properties with backing fields. And NO default constructor is generated.

I think that the compiler does not work this way with anonymous types for Linq2Sql


Since a constructor with parameters can have any behavior, Linq2Sql cannot make any assumption on that so it do must execute the new() in memory.
With a type with only auto-properties Linq2Sql can make assumption about the behavior and for example compute a join in the Db instead of loading both sets and creating the instances in memory.


This refactoring with some Linq2Sql code (especially a Join between 2 anonymous type, when you try to extract one side of the join) do change the behavior of the code unless you chose only auto-properties

0

Please sign in to leave a comment.