This question is continuation of Getting Count() property in Dynamic Lambda Expression
I had asked if we can put Count() Method in dynamic lambda expression. I could do it using Dynamic Expression API. Answer provided by - xmojmr
But recently I had to implement a Dynamic Lambda Expression to get result with its child entity with filtered data.
Details :
I have a parent entity with its child entity linked. Now when I get data from parent entity, it returns a collection of data with its child data too.
as per my previous requirement I had to count all child record returned with respect to each parent record.
I had done it using -
ParameterExpression cParam = Expression.Parameter(typeof(CPNDBase), "c");
NewExpression newExp = Expression.New(typeof(DTDataModel));
List bindings = new List();
MemberInfo memberInfo = typeof(DTDataModel).GetMember("FILE_COUNT")[0];
Dictionary paramExSymbols = new Dictionary();
paramExSymbols.Add("c", cParam);
Expression expression = System.Linq.Dynamic.DynamicExpression.Parse(null, "c.CPNDocs.Count()", paramExSymbols);
MemberBinding memberBinding = Expression.Bind(memberInfo, expression);
bindings.Add(memberBinding);
MemberInitExpression memberInitExpression = System.Linq.Expressions.Expression.MemberInit(newExp, bindings);
Expression> selector = (Expression>)BinaryExpression.Lambda(memberInitExpression, cParam);
var finalFilteredCPNData = filteredCPNData.AsQueryable().Select(selector);
Here c refrerence to parent resultset and c.CPNDocs.Count() trying to count records in CPNDocs which is child entity resultset.
I could achieved that using Dynamic Expression API.
Now my recent need is to modify my previous requirement and I need to generate Dynamic Expression for
c.CPNDocs.Where(a => a.L_STAT == true).Count()
Here before count I need to filter its data based on its member variable. I have tried with Dynamic Expression API but getting error for Lambda expression. Not able to build it.
Can somebody help me with this?