Question

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?

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top