c# - Assign an expression to a property inside the expression tree -
is there way assign 'result' of selector expression property of selector inside expression tree?
to clarify: i'd transform result set retrieved database data transfer object using selector expression. example this:
expression<func<parent, parentdto>> selector = parent => new parentdto { children = parent.children.select(x => new childdto())...
this doesn't work linq entities children
(property on entity parent
) ienumerable<t>
without initializer. case, have static expression accessor (public static expression<func<parent, icollection<child>>> childrenaccessor = f => f.internalchildrenstorage
) returns internal collection (icollection<t>
). need to:
- create dto mapping expression child entities
- create dto mapping expression parent entity (parent entity)
- replace parameters of static accessor ones child entities mapping expression
- combine parent mapping expression child entities mapping expression
in end, should (pseudo) this:
expression<func<parent, parentdto>> selector = parent => new parentdto { children = parent.internalchildrenstorage.select(x => new childdto())...
here's got (realworld code):
// parent = setupgroup // child = setupmap expression<func<icollection<setupmap>, ienumerable<setupmapdto>>> transformmapstodto = maps => maps.select(x => new setupmapdto { camera = new setupmapcameradto { id = x.camera.id, friendlyname = x.camera.friendlyname }, display = new setupmapdisplaydto { id = x.display.id, friendlyname = x.display.friendlyname } }).where(x => x.camera != null); var mapsselector = setupgroup.setupmapsaccessor.combine(transformmapstodto); expression<func<setupgroup, setupgroupreportdto>> transformgroupstodto = @group => new setupgroupreportdto { grouptext = @group.grouptext, groupnumber = @group.groupnumber }; expression<func<setupgroupreportdto, ienumerable<setupmapdto>>> dtomapsselector = dto => dto.setupmaps; var parameterexpression = (parameterexpression) (((memberexpression) (dtomapsselector.body)).expression); var assignment = expression.lambda<action<setupgroupreportdto>>(expression.assign(dtomapsselector.body, mapsselector.body), parameterexpression); var resultdtoblock = expression.block(transformgroupstodto, assignment); var selectorparameter = expression.parameter(typeof (setupgroup)); var dtoselector = expression.lambda<func<setupgroup, setupgroupreportdto>>(resultdtoblock, selectorparameter); var results = _setupgrouprepository.getall(dtoselector);
this doesn't seem work second part of block expression (the assignment) doesn't combined transformgroupstodto
expression. did misunderstand purpose of expression.block
?
Comments
Post a Comment