在动态Lambda表达式中获取Count()属性 [英] Getting Count() property in Dynamic Lambda Expression

查看:4927
本文介绍了在动态Lambda表达式中获取Count()属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎得到了我需要的东西,但只有一个我坚持住的地方。我需要在Lambda表达式中动态地构建 fileCount = c.CPNDocs.Count()。代码在下面,注释我用来构建动态Lambda表达式。

  var dColDefaultList = new List< String>() 下载,I_ID,C_TYP,C_LST_ACT}; //< -------我需要的Lambdas表达式

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

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

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

foreach(dColDefaultList中的String sCol)
{
if(!String.Equals(sCol,下载)){
bindings.Add(GetMemberBinding(sCol ,cParam,sCol));
}
else
{
bindings.Add(GetMemberBinding(fileCount,cParam,CPNDocs.Count())); //< -------需要从CPNDocs返回的行数(不同的表)是我从Entity Relatioship收到的对象
}
}

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

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

//选择器将为selector = {c =>新的DTDataModel(){fileCount = c.CPNDocs,I_ID = c.I_ID,C_TYP = c.C_TYP,C_LST_ACT = c.C_LST_ACT}}
//但是我需要selector = {c =>新的DTDataModel(){fileCount = c.CPNDocs.Count(),I_ID = c.I_ID,C_TYP = c.C_TYP,C_LST_ACT = c.C_LST_ACT}}

//问题是我如何fileCount = c.CPNDocs.Count()?

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

上面的方法定义在这里:

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

有人知道我该怎么做?

解决方案

Count()不是属性。它是一个在静态类中实现的扩展方法。这种扩展方法在几个地方实现。正确的地方取决于你的班级继承。要找到正确的地方,您使用Visual Studio的定义功能。



例如。对于 IQueryable.Count()扩展方法由 System.Linq.Queryable 静态类实现,可以在这里看到→ http://referencesource.microsoft.com/#System.Core /System/Linq/IQueryable.cs



所以为了编码表达式,你需要对扩展方法的一个调用进行编码。

$由Microsoft发布的一个原型很早就显示了从字符串生成表达式树的简单方法。介绍性文章有在 LINQ中的动态表达式和查询



我们使用自动string to linq引擎的原始源的修改版本成功,它简化了开发。通过检查 System.Linq.Dynamic 的源代码,您可以确定如何对表达式进行编码。链接到可通过NuGet获得的原始源代码。堆栈溢出文章动态LINQ - 有.NET 4版本吗? / a>


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?

解决方案

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.

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

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

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

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表达式中获取Count()属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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