在动态LAMBDA防爆pression获得计数()属性 [英] Getting Count() property in Dynamic Lambda Expression

查看:107
本文介绍了在动态LAMBDA防爆pression获得计数()属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎得到了我所需要的,但只有一个地方,我被卡住。我需要建立 fileCount = c.CPNDocs.Count()在动态LAMBDA防爆pression。 code以下是我所使用建立动态LAMBDA前pression意见。

I almost got what I need but only one place where I stucked. I need to build fileCount = c.CPNDocs.Count() Dynamically in Lambda Expression. Code is below with comments what I am using to build Dynamic Lambda Expression.

var dColDefaultList = new List<String>() { "Download", "I_ID", "C_TYP", "C_LST_ACT" };       // <------- Columns I need in Lambdas Expression

ParameterExpression cParam = Expression.Parameter(typeof(CPNDBase), "c");

NewExpression newExp = Expression.New(typeof(DTDataModel));

List<MemberBinding> bindings = new List<MemberBinding>();

foreach (String sCol in dColDefaultList)
{
    if (!String.Equals(sCol, "Download")) {
        bindings.Add(GetMemberBinding(sCol, cParam, sCol));
    }
    else
    {
        bindings.Add(GetMemberBinding("fileCount", cParam, "CPNDocs.Count()")); //   <-------need count of rows return from CPNDocs(Different Table) is a Object I recieved from     Entity Relatioship
    }
}

MemberInitExpression memberInitExpression =         System.Linq.Expressions.Expression.MemberInit(newExp, bindings);

Expression<Func<CPNDBase, DTDataModel>> selector = (Expression<Func<CPNDBase,   DTDataModel>>)BinaryExpression.Lambda(memberInitExpression, cParam);

// selector  will be selector = {c => new DTDataModel() {fileCount = c.CPNDocs, I_ID =   c.I_ID, C_TYP = c.C_TYP, C_LST_ACT = c.C_LST_ACT }}
// but I Need selector = {c => new DTDataModel() {fileCount = c.CPNDocs.Count(), I_ID = c.I_ID, C_TYP = c.C_TYP, C_LST_ACT = c.C_LST_ACT }}

// Question is How can I make fileCount = c.CPNDocs.Count() ?

var resultLm = finalFilteredCPNData.AsQueryable<CPNDBase>().Select(selector);

以上方法在这里被定义:

Above method is defined here :

static MemberBinding GetMemberBinding(string property, ParameterExpression param,  string column)
{
    MemberInfo memberInfo = typeof(DTDataModel).GetMember(property)[0];
    MemberExpression memberExpression = LambdaExpression.PropertyOrField(param, column);
    return System.Linq.Expressions.Expression.Bind(memberInfo, memberExpression);
}

有谁知道我能做到这一点?

Does anybody know how can I do this?

推荐答案

计数()不是属性。它是一个静态类实现的扩展方法。这个扩展方法在几个地方实施。正确位置取决于你有什么类继承。要找到您使用Visual Studio的转到定义功能正确的位置。

The Count() is not a property. It is an extension method implemented in a static class. This extension method is implemented at several places. Correct place depends on what are your classes inheriting from. To find the correct place you use the "go to definition" feature of Visual Studio.

例如。为 IQueryable.Count()的扩展方法是由 System.Linq.Queryable 静态类实现为可以看到这里→ http://referencesource.microsoft.com/#System.Core/系统/ LINQ的/ IQueryable.cs

e.g. for IQueryable.Count() the extension methods are implemented by System.Linq.Queryable static class as can be seen here → http://referencesource.microsoft.com/#System.Core/System/Linq/IQueryable.cs

因此​​,为了EN code你需要连接code将扩展方法调用的前pression。

So in order to encode the expression you need to encode a call to the extension method.

更易于生成字符串的前pression树木的方式被证明由微软发布的原型相当早。介绍性的文章可如在<一个href=\"http://conf.oreodor.com/download/attachments/2261145/Dynamic%2BEx$p$pssions%2Band%2BQueries%2Bin%2BLINQ.doc\"相对=nofollow>动态防爆pressions和查询在LINQ

Much simpler way to generate expression trees from strings was shown quite early in a prototype published by Microsoft. Introductory article is available e.g. in Dynamic Expressions and Queries in LINQ

我们使用自动的最初来源改装版字符串LINQ成功的发动机和它简​​化了开发了很多。通过检查 System.Linq.Dynamic源$ C ​​$ C 你可以找到准确的路该怎么EN code除权pression。链接到原始的源$ C ​​$ C可通过的NuGet被提到例如在堆栈溢出文章动态LINQ - 是否有.NET 4版本<? / A>

We use modified version of the original source of the automatic "string to linq" engine with success and it simplifies development a lot. By inspecting the source code of the System.Linq.Dynamic you can find exact way how to encode the expression. Link to the original source code available through NuGet is mentioned e.g. in Stack Overflow article Dynamic LINQ - Is There A .NET 4 Version?

这篇关于在动态LAMBDA防爆pression获得计数()属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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