2

This question is continuation of Getting Count() property in Dynamic Lambda Expression

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

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

Details :

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.

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

I had done it using -


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

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

I could achieved that using Dynamic Expression API.

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

c.CPNDocs.Where(a => a.L_STAT == true).Count()

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.

Can somebody help me with this?

Community
  • 1
  • 1
dPatel1582
  • 124
  • 10
  • 1
    your examples look somehow overcomplicated. Do you really have to build expressions dynamically in code? Would not the built-in C# linq support be enough for you? (take a look e.g. here: http://www.dotnetperls.com/linq) – xmojmr May 13 '14 at 14:05
  • Yes Xmojmr, I have to build Dynamic Expression as we are using dynamic columns in the code. If we have other(Static) way of filtering data of Child Entity Object and get final Parent Entity with its filtered child, I can left rest of the code as it is. I have tried filtering using linq but I had to provide static columns to get final parent entity. But in my case I do not want to use static column. – dPatel1582 May 13 '14 at 14:46
  • `Where` is also extension for `IQueryable` as well as `Count` – Grundy Jun 09 '14 at 07:48

1 Answers1

1

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

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

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.

So I replaced c.CPNDocs.Count() in

Expression expression = System.Linq.Dynamic.DynamicExpression.Parse(null, "c.CPNDocs.Count()", paramExSymbols);

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 => (Param_0.L_STAT == True)).Count()

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, you last answer was the answer for this question too. Thanks.

dPatel1582
  • 124
  • 10
  • 1
    you are kind but **ALL CREDITS for correct answer are yours**. For the Linq with dynamic columns you may find useful [Coolest C# LINQ/Lambdas trick you've ever pulled?](http://stackoverflow.com/questions/28858/coolest-c-sharp-linq-lambdas-trick-youve-ever-pulled), [LINQ - Projection Operators](http://code.msdn.microsoft.com/LINQ-to-DataSets-09787825) and [MSDN: Queries in LINQ to DataSet](http://msdn.microsoft.com/en-us/library/bb552415(v=vs.110).aspx). DataSet's use dynamic column names defined as strings so this one may be close. And you can chain query operators together.. – xmojmr May 13 '14 at 17:08