score:2
Accepted answer
I'm not familiar with the System.Linq.Dynamic
library, but assuming it produces normal, strongly typed Expression
trees you can use an ExpressionVisitor
.
This one counts the number of boolean logical operations like &&
:
int CountConditions(Expression expr)
{
var visitor = new CountBinaryOpsVisitor();
visitor.Visit(expr);
return visitor.BinaryOperationCount + 1;
}
class CountBinaryOpsVisitor : ExpressionVisitor
{
public int BinaryOperationCount { get; private set; }
protected override Expression VisitBinary(BinaryExpression node)
{
switch (node.NodeType)
{
case ExpressionType.And:
case ExpressionType.AndAlso:
case ExpressionType.Or:
case ExpressionType.OrElse:
case ExpressionType.ExclusiveOr:
// Don't count bitwise integer operations, if they are even supported?
if (node.Left.Type == typeof(bool))
BinaryOperationCount++;
break;
}
return base.VisitBinary(node);
}
}
An alternative approach would be to count the number of comparison operators (==
, >=
etc.), but I think that would need more complex code to handle boolean expressions like x.BooleanProp
or x.Insurers.Any()
.
This implementation doesn't currently count conditional expressions (x ? y : z
). Not sure how you would factor those into the number of conditions, especially when nested.
Source: stackoverflow.com
Related Articles
- Identify number of conditions in a dynamic LINQ expression
- Creating dynamic linq expression OData for multiple conditions for filter Contains?
- Linq to XML with n number of dynamic where conditions
- How to create dynamic LINQ Expression with multiple binary expression conditions and a contains condition
- Dynamic LINQ OR Conditions
- How to build a dynamic AND OR linq expression tree in a loop
- passing dynamic expression to order by in code first EF repository
- Identify an event via a Linq Expression tree
- Dynamic linq query expression tree for sql IN clause using Entity framework
- How to reuse a linq expression for 'Where' when using multiple source tables
- Dynamic Linq Expression for IEnumerable<int>.contains(MemberExpression)
- Null Reference Exception in a Dynamic LINQ Expression
- Dynamic LINQ Expression for sorting navigation property
- LINQ Source Code Available
- Generate dynamic LINQ expression based on array
- how to create a pivot table with dynamic column using linq tree expression
- How to create a dynamic 'contains or LIKE' Expression to be used with Linq against OData service
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- LINQ Dynamic Expression API, predicate with DBNull.Value comparison
- Create a Dynamic Linq to EF Expression to Select IQueryable into new class and assign properties
- add dynamic anonymous object into linq groupBy expression
- Build dynamic linq to Sql lambda expression
- Dynamic LINQ expression where field name is not known
- Dynamic linq Building Expression
- Looking for simple way to build Linq dynamic / conditional expression
- LINQ - Dynamic expression to OrderBy
- Dynamic LINQ Expression
- Dynamic linq expression tree with nested properties
- creating Linq to sqlite dbml from DbLinq source code
- Creating dynamic linq expression tree against nested OData
- Linq query over mysql table takes too long
- Linq to EF - populate a (fairly simple) hierarchical object from EDM C#
- Can't insert NULL into varbinary column using LINQ
- Why .Concat method's return value type looks a bit weird: System.Linq.Enumerable+(...)?
- how to return the query result as a specific object type in LINQ
- Get node value xml with LINQ c#
- Linq To entities, Select from select
- EF Core Include childs after query with joins
- How to access a property of a custom class using Linq to Object?
- How to query a XML with namespaces in the name?
- Using LINQ to group and count occurrences from dictionary
- How to dynamically get a property value in which is in another property with Linq where statement
- What is the best way to test a condition in Linq to NHibernate?
- Linq To Xml Null Checking of attributes
- Does LINQ with a scalar result trigger the lazy loading
- Using Linq-to-XML to insert, with threading
- how do I pass null to int column in linq
- Include null cells in Linq query from DB in C# code
- SQL to LINQ possible?
- Avoiding repeated projection code in Entity Framework