2

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?

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
dPatel1582
  • 124
  • 10
  • The code looks quite "ugly". Did you consider using http://stackoverflow.com/questions/5163147/dynamic-linq-is-there-a-net-4-version – xmojmr Apr 30 '14 at 18:37
  • I am trying to get All fields from Parent Table with a Custom Column in it having counts of all Child record with respect to each record in Parent. Link explains all about filtering, but I am trying something else. Please let me know if I am missing something from the link you provided. – dPatel1582 Apr 30 '14 at 19:23

1 Answers1

1

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?

Community
  • 1
  • 1
xmojmr
  • 8,073
  • 5
  • 31
  • 54
  • Thanks a lot **xmojmr**, Actually I am new to these technologies, It was hard to understand all of these. but some how I catch the right way by refereing your [link](http://conf.oreodor.com/download/attachments/2261145/Dynamic%2BExpressions%2Band%2BQueries%2Bin%2BLINQ.doc). It is really Help full. and finally I was able to attach a Count field with resultset. Worderful Help. Thanks a lot once again. Your answer is really helpful. – dPatel1582 May 01 '14 at 15:39
  • Hello **xmojmr**, I think you may help me with this - http://stackoverflow.com/questions/23632811/get-an-entity-object-with-its-child-entity-with-a-condition-using-dynamic-linq , I am trying to filter data before its count() using Dynamic Expression, but failed to generate. Could you please help me with this. Thank you. – dPatel1582 May 13 '14 at 13:38