I now have about this code:
public override Expression<Func<ITask, TaskDto>> ConvertEntityToDto() { return t => t.TypeDiscriminator == guid ? new SimpleTaskDto((ISimpleTask)t) : new TaskDto(t); } It works fine, sometimes it returns me SimpleTaskDto , sometimes TaskDto , everything is as planned.
In fact, I want to do a little harder - return the FooTaskDto fooGuid-FooTaskDto pairs that are known in the runtime, and the TaskDto to the unknown TaskDto .
Can this be built through Expression? If not, what options are there?
The bottom line is that this is part of transforming entities from the database into Dto, and if I choose the wrong type, then I lose some useful fields, which makes it necessary to make a separate query, although I could do it right here.
t.TypeDiscriminatorknown statically? Well, you do not want to lookup then hold it at the base level? - VladDt => (t.TypeDiscriminator == guid1) ? new SimpleTaskDto((ISimpleTask)t) : (t.TypeDiscriminator == guid2) ? new FooTaskDto((IFooTask)t) : new TaskDto(t)t => (t.TypeDiscriminator == guid1) ? new SimpleTaskDto((ISimpleTask)t) : (t.TypeDiscriminator == guid2) ? new FooTaskDto((IFooTask)t) : new TaskDto(t)t => (t.TypeDiscriminator == guid1) ? new SimpleTaskDto((ISimpleTask)t) : (t.TypeDiscriminator == guid2) ? new FooTaskDto((IFooTask)t) : new TaskDto(t)? - VladD