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

我的未来我决定 提交于 2019-12-22 11:17:59

问题


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:


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.



来源:https://stackoverflow.com/questions/23632811/get-an-entity-object-with-its-child-entity-with-a-condition-using-dynamic-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!