linq - getting at a deep property for a lambda Expression -
i have code thus:
caseheadercomparer casecomparer = new caseheadercomparer(); list<caseheader> casestoprocess = new list<caseheader>(); foreach (groupfield fld in fields) { //get field property - ie. division system.reflection.propertyinfo pifield = typeof(caseheader).getproperty(fld.groupfieldtype.propertyname); //get item property - ie. divisionid system.reflection.propertyinfo piitem = pifield.propertytype.getproperty(fld.groupfieldtype.valuemember); foreach (caseheader ch in toprocess) { object chitem = pifield.getvalue(ch, null); guid itemid = chitem != null ? (guid)piitem.getvalue(chitem, null) : guid.empty; if (fld.items.select(i => i.itemid).contains(itemid)) { casestoprocess.add(ch); } } toprocess = toprocess.except(casestoprocess, casecomparer).tolist(); }
which convert to use linq , lambdas - got close here yesterday this:
list<caseheader> toprocess = ....; caseheadercomparer casecomparer = new caseheadercomparer(); ienumerable<caseheader> casestoprocess = new backingsheetcaseheader[] { }; foreach (groupfield fld in fields) { //get field property - ie. division system.reflection.propertyinfo pifield = typeof(caseheader).getproperty(fld.groupfieldtype.propertyname); //get item property - ie. divisionid system.reflection.propertyinfo piitem = pifield.propertytype.getproperty(fld.groupfieldtype.valuemember); casestoprocess.union( toprocess .where(c => fld.items.select(i => i.itemid) .contains((pifield.getvalue(c, null) != null ? (guid)piitem.getvalue(pifield.getvalue(c, null), null) : guid.empty))) , casecomparer); }
this works pointed out along lines of this..
var hdr = typeof(caseheader); var param = expression.parameter(hdr); var cond = expression.condition( expression.notequal(param, expression.constant(null, hdr)) , expression.property(param, fld.groupfieldtype.propertyname) <<-- needs go 2 deep.. above item property.. , expression.constant(guid.empty) ); var lambda = (func<mycaseobj,guid>)expression.lambda(cond, param).compile();
then
var casestoprocess = (from csh in casesingroup lambda(csh).in(fld.items.select(i => i.itemid)) select csh);
but 2 deep bit stumping me above need fld.groupfieldtype.valuemember property of fld.groupfieldtype.propertyname property of caseheader.. the value of first level may null..
can give me pointers, or somewhere have read on this
thanks
it may don't need build expression
manually @ all. c# compiler can translate lambda expressions func<>
s and expression<func<>>
s. both following work:
var func = (func<int, bool>)(i => == 2); var expression = (expression<func<int, bool>>)(i => == 2);
after expression.compile()
func<int,bool>
same behaviour func
! try just
var expression = (expression<func<mycaseobj,guid>>) (c => fld.items.select(i => i.itemid) .contains((pifield.getvalue(c, null) != null ? (guid)piitem.getvalue(pifield.getvalue(c, null), null) : guid.empty)) );
and take there.
Comments
Post a Comment