question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] Dynamic OR clause in where statement

See original GitHub issue

How to create the dynamic query with OR clause. For example I have 2 tables Orders, OrderLines and I need to create dynamic OR filter. Small example with pseudo code.

var query = (
    from o in db.Orders
    from ol in db.OrderLines.Where(ol => o.OrderId == ol.OrderId)
    select new QueryData { Order = o, OrderLine = ol } );

if(status > 0)
     query = query.Where(arg => arg.Order.Status == status );

var orFilters = new List<Expression<Func<QueryData, bool>>>();

if(filters1.Count > 0)
    orFilters.Add(CreateFilter1(filters1))
if(filters2.Count > 0)
    orFilters.Add(CreateFilter2(filters2))


query = query.Or(orFilters);


class QueryData 
{
    Order Order { get; aet; } 
    OrderLines OrderLines { get; aet; } 
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Shane32commented, Jul 10, 2020

In your example, it would be used like this:

query = query.Where(orFilters.Or());
0reactions
sdanylivcommented, Jul 16, 2020

Closing as answered.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Building dynamic where condition in SQL statement
An expression of non-boolean type specified in a context where a condition is expected, near 'set'.
Read more >
Implementing Dynamic WHERE-Clause in Static SQL
You can see in example 2.2 - WHERE -clause is built dynamically using the ISNULL function. It evaluates the expression and checks whether...
Read more >
Dynamic SQL in SQL Server
Dynamic SQL is the SQL statement that is constructed and executed at runtime based on input parameters passed. Let us go through some...
Read more >
Providing variable input to dynamically executed SQL ...
In a dynamic SQL statement, parameter markers are used instead of host variables. A parameter marker is indicated by a question mark (?)...
Read more >
Sql based on dynamic where condition
1 Answer 1 · There are multiple conditions based on parameter. In this example 3 different conditions i.e. equal to or greater than...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found