获取与其子实体的实体对象与条件(使用动态LINQ) [英] Get an Entity Object with its child Entity with a condition (using Dynamic Linq)

查看:129
本文介绍了获取与其子实体的实体对象与条件(使用动态LINQ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题是<延续href=\"http://stackoverflow.com/questions/23391222/getting-count-property-in-dynamic-lambda-ex$p$pssion\">Getting算上动态LAMBDA防爆pression()属性

我曾问我们是否可以把伯爵动态拉姆达前pression()方法。我能做到这一点使用的动态防爆pression API 。回答提供 - xmojmr

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

但最近我不得不实施动态LAMBDA防爆pression获得与过滤后的数据其子实体的结果。

But recently I had to implement a Dynamic Lambda Expression to get result with its child entity with filtered data.

详细内容:

我有联系的子实体父实体。现在,当我从父实体数据,它返回的数据的集合,其子数据了。

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.

按我的previous要求我必须计算所有子记录对每个父记录返回。

as per my previous requirement I had to count all child record returned with respect to each parent record.

我已经用它做 -


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);

下面的 C refrerence于母公司的ResultSet和 c.CPNDocs.Count()试图计算记录CPNDocs这是子实体结果集。

Here c refrerence to parent resultset and c.CPNDocs.Count() trying to count records in CPNDocs which is child entity resultset.

我可以实现,使用的动态防爆pression API

现在我最近需要的是修改我的previous要求,我需要生成动态防爆pression为

Now my recent need is to modify my previous requirement and I need to generate Dynamic Expression for

c.CPNDocs.Where(A =&GT; a.L_STAT == true)而.Count之间的()

下面计数之前,我需要根据其成员变量来过滤数据。
我曾尝试与动态防爆pression API ,但得到的LAMBDA前pression错误。无法建立它。

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.

有人可以帮助我?

推荐答案

我想出如何动态防爆pression API 的作品。正如我说我是新来的这些技术,我还在学习的东西。

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

我不得不实现 - c.CPNDocs.Where(D =&GT; d.L_STAT == true)而.Count之间的()

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

我想我们必须写在一个字符串前pression和动态防爆pression API 将其解析到一个真正的防爆pression,但是我错了。我们实际上要告诉解析方法的动态防爆pression API 的是,我们想要做的。

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.

所以我换成 c.CPNDocs.Count()

防爆pression前pression = System.Linq.Dynamic.DynamicEx pression.Parse(NULL,c.CPNDocs.Count(),paramExSymbols)​​;

c.CPNDocs.Where(L_STAT ==真).Count之间的()。注意:在其中,子句,它不具有防爆LAMBDA pression而不仅仅是一个条件。 解析方式将巧妙地了解我试图做的,并将其与动态引用转换为防爆pression并生成一个Ex pression完全相同正如我一直在寻找 -

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 =&GT;(Param_0.L_STAT ==真))。COUNT()

其中的 Param_0 是CPNDoc创建动态引用(CPNDocs一个记录实例)。
所以,最后的结果我得到了我所期待的。

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,你最后的回答是这个问题的答案了。谢谢你。

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

这篇关于获取与其子实体的实体对象与条件(使用动态LINQ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆