score:2
Accepted answer
What you have to do is break down the original IQueryable
, extract the source and query expression and then build a new query expression and then a new IQueryable from the source and the new query expression. If there is no Where
, just add the condition to the original query.
IQueryable<T> q = _context.Set<T>().Where(x => x.Id == 1);
if(someValue) {
Expression<Func<T,bool>> newWhereClause = (T x) => x.Status == 1;
Expression source;
if (q.Expression is MethodCallExpression qe && qe.Method.Name == "Where") {
var we = (MethodCallExpression)q.Expression; // get the call to Where
var wea1 = (UnaryExpression)we.Arguments[1]; // get the 2nd arg to Where (Quoted Lambda)
var leftExpr = (Expression<Func<T, bool>>)wea1.Operand; // Extract the lambda from the QuoteExpression
newWhereClause = ExpressionBuilder.Or(leftExpr, newWhereClause);
q = q.Provider.CreateQuery<T>(we.Arguments[0]).Where(newWhereClause);
}
else
q = q.Where(newWhereClause);
}
Note that this depends on the internals of LINQ and expression trees, and could break at some point in the future.
Source: stackoverflow.com
Related Articles
- How to extract a where clause expression tree from IQueryable
- Selectively remove from where clause in LINQ expression tree
- Mapping IQueryable where clause from DTO to Entity
- How to get the where clause from IQueryable defined as interface
- LINQ Expression - Dynamic From & Where Clause
- Lambda Expression - How to provide values to where clause from an IEnumerable<Object>?
- Get lambda expression from where clause or IQueryable/IEnumerable
- Expression tree for groupby with where clause and than select
- LINQ where clause using Generic IQueryable source
- Where clause using Expression tree builder
- c# Linq or code to extract groups from a single list of source data
- Entity Framework dynamic linq where from generic source with dynamic where clause
- How can I pull a repeated where clause expression from linq into a function?
- IQueryable where clause generated from list that needs to be OR'd together
- lambda expression join multiple tables with select and where clause
- Async Await in Lambda expression where clause
- Dynamic linq query expression tree for sql IN clause using Entity framework
- Passing Func to Where changes return type from IQueryable to IEnumerable
- Extract method name from expression tree?
- Building a dynamic where clause for dynamic keywords or using IQueryable C# Linq
- Execute expression on another IQueryable source
- Where clause not working on LINQ IQueryable query
- How to call a method in the where clause of a LINQ query on a IQueryable object
- Opinions on Expression Tree Serialization Library from CodePlex?
- Translating expression tree from a type to another type with complex mappings
- How to negate a Where clause of an IQueryable
- Linq with where clause in many-to-many EF Code First object
- Generic expression for where clause - "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities."
- Combine property selector expression tree and value to create a predicate for EF filtering - create filter from lambda selector and value
- Traverse an expression tree and extract parameters
- Get textbox with values and Checked Checkboxes from formcollection MVC 4
- C# - Generic method with linq parameter
- Having a little trouble converting this SQL script to LINQ
- Group join Linq C#
- linq: how to divide a list into two by the sum of the values and keeping the input order
- how to return array from linq?
- Linq using sum() in list within a list
- LINQ query join only last value
- How can I sort a datatable using an outside list (prefer linq)?
- Linq security - hide columns
- List<MyObject> Contains
- Resolving method overload among LINQ query methods
- Querying a list in a list efficently
- EntityFramework - How to populate child members?
- Sort XML document based on grandchild
- Linq query results to List collection
- SQL query that looks within the same table
- How to insert only new records using Linq-to-SQL?
- LINQ OrderBy on inner object's property
- How to use XML and LINQ