Get an Entity Object with its child Entity with a condition (using Dynamic Linq)

asp.net-mvc-4 c# dynamic-linq entity-framework-5 lambda

Question

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?

1
2
5/23/2017 12:27:13 PM

Popular Answer

I figured out how Dynamic Expression API works. As I said I am new to these technologies, I am still learning things.

I had to achieve - c.CPNDocs.Where(d => d.L_STAT==true).Count()

I thought we have to write an expression in string and Dynamic Expression API will parse it to a real Expression, But I was wrong. we actually have to tell Parse method of Dynamic Expression API that what we want to do.

So I replaced c.CPNDocs.Count() in

Expression expression = System.Linq.Dynamic.DynamicExpression.Parse(null, "c.CPNDocs.Count()", paramExSymbols);

with c.CPNDocs.Where(L_STAT==true).Count(). Note the Where clause, It does not has LAMBDA Expression instead only a condition. Parse method will smartly understand what I am trying to do and convert it to an Expression with a dynamic reference and generate an Expression exactly same as what I was looking for -

c.CPNDocs.Where(Param_0 => (Param_0.L_STAT == True)).Count()

Where Param_0 is a Dynamic reference created for CPNDoc(an Record instance in CPNDocs). So final result I got what I was looking for.

Xmojmr, you last answer was the answer for this question too. Thanks.

1
5/13/2014 3:17:06 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow