Linq
C#
lambda expressions
programming
.NET

Or equivalent in Linq Where lambda expression

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In LINQ, the || (OR) operator in a Where lambda works exactly like in regular C# code. You can combine multiple conditions with || inside a single Where, chain multiple Where calls (which produces AND, not OR), or build dynamic OR conditions using PredicateBuilder or expression trees when the conditions are not known at compile time.

Basic OR in Where

csharp
1var numbers = new List<int> { 1, 2, 3, 4, 5, 10, 15, 20 };
2
3// OR condition — either less than 5 or greater than 15
4var result = numbers.Where(n => n < 5 || n > 15).ToList();
5// [1, 2, 3, 4, 20]
6
7// Multiple OR conditions
8var result2 = numbers.Where(n => n == 1 || n == 5 || n == 20).ToList();
9// [1, 5, 20]

OR with Objects

csharp
1var products = new List<Product>
2{
3    new("Widget", 10, "Electronics", true),
4    new("Gadget", 0, "Electronics", false),
5    new("Doohickey", 5, "Clearance", true),
6    new("Thingamajig", 0, "Seasonal", true),
7};
8
9// Products that are out of stock OR discontinued
10var result = products.Where(p => p.Stock == 0 || !p.IsActive).ToList();
11
12// Products in Clearance OR Electronics category
13var result2 = products.Where(p => p.Category == "Clearance" || p.Category == "Electronics").ToList();

OR with Contains (IN equivalent)

For checking against a list of values, use Contains instead of chaining ||:

csharp
1var categories = new[] { "Electronics", "Clearance", "Seasonal" };
2
3// Instead of: p.Category == "Electronics" || p.Category == "Clearance" || ...
4var result = products.Where(p => categories.Contains(p.Category)).ToList();

In LINQ-to-SQL/EF, this translates to WHERE Category IN ('Electronics', 'Clearance', 'Seasonal').

Combining AND and OR

csharp
1// (price > 100 AND inStock) OR isFeatured
2var result = products.Where(p =>
3    (p.Price > 100 && p.InStock) || p.IsFeatured
4).ToList();
5
6// Active products that are either cheap OR on sale
7var result2 = products.Where(p =>
8    p.IsActive && (p.Price < 10 || p.OnSale)
9).ToList();

Use parentheses to control precedence — && binds tighter than || in C#.

WARNING: Chaining Where is AND, Not OR

csharp
1// This is AND, not OR!
2var result = numbers
3    .Where(n => n < 5)
4    .Where(n => n > 15)
5    .ToList();
6// Result: [] (empty) — no number is both < 5 AND > 15
7
8// For OR, use a single Where with ||
9var result = numbers
10    .Where(n => n < 5 || n > 15)
11    .ToList();
12// [1, 2, 3, 4, 20]

Dynamic OR with PredicateBuilder

When OR conditions are built at runtime (e.g., from user-selected filters):

csharp
1// Using LINQKit's PredicateBuilder
2using LinqKit;
3
4var predicate = PredicateBuilder.New<Product>(false); // false = start with "no match"
5
6if (filterByElectronics)
7    predicate = predicate.Or(p => p.Category == "Electronics");
8
9if (filterByClearance)
10    predicate = predicate.Or(p => p.Category == "Clearance");
11
12if (filterByLowStock)
13    predicate = predicate.Or(p => p.Stock < 5);
14
15var result = products.AsQueryable().Where(predicate).ToList();

Install with: dotnet add package LinqKit.Microsoft.EntityFrameworkCore

Manual Expression Building (No External Library)

csharp
1using System.Linq.Expressions;
2
3public static Expression<Func<T, bool>> OrElse<T>(
4    Expression<Func<T, bool>> left,
5    Expression<Func<T, bool>> right)
6{
7    var param = Expression.Parameter(typeof(T));
8    var body = Expression.OrElse(
9        Expression.Invoke(left, param),
10        Expression.Invoke(right, param));
11    return Expression.Lambda<Func<T, bool>>(body, param);
12}
13
14// Usage
15Expression<Func<Product, bool>> filter = p => p.Stock == 0;
16filter = OrElse(filter, p => !p.IsActive);
17filter = OrElse(filter, p => p.Category == "Clearance");
18
19var result = dbContext.Products.Where(filter).ToList();

LINQ Query Syntax

csharp
1// Query syntax equivalent
2var result = from n in numbers
3             where n < 5 || n > 15
4             select n;
5
6// With objects
7var result2 = from p in products
8              where p.Stock == 0 || p.Category == "Clearance"
9              select p;

Entity Framework Considerations

csharp
1// EF translates || to SQL OR
2var result = dbContext.Products
3    .Where(p => p.Price > 100 || p.IsFeatured)
4    .ToList();
5// SQL: SELECT * FROM Products WHERE Price > 100 OR IsFeatured = 1
6
7// Contains translates to IN
8var ids = new[] { 1, 5, 10 };
9var result2 = dbContext.Products
10    .Where(p => ids.Contains(p.Id))
11    .ToList();
12// SQL: SELECT * FROM Products WHERE Id IN (1, 5, 10)

Common Pitfalls

  • Chaining Where is AND: list.Where(a).Where(b) means a AND b, not a OR b. For OR, combine conditions in a single Where with ||.
  • Operator precedence: && binds tighter than ||. a || b && c means a || (b && c). Use parentheses explicitly: (a || b) && c.
  • Null reference in OR: p.Name != null || p.Name.Contains("test") — the right side still executes if the left is false. Use: p.Name != null && p.Name.Contains("test") or null-conditional: p.Name?.Contains("test") == true.
  • Dynamic OR with EF: Building OR conditions dynamically requires expression trees (PredicateBuilder). String concatenation of SQL is not an option with LINQ.
  • Short-circuit evaluation: In LINQ-to-Objects, || short-circuits (skips right side if left is true). In LINQ-to-SQL/EF, the database evaluates both sides. Be aware of this difference for performance-sensitive queries.

Summary

  • Use || inside a Where lambda for OR conditions: .Where(x => a || b)
  • Chaining .Where(a).Where(b) produces AND, not OR
  • Use Contains() for IN-style checks instead of chaining multiple ||
  • Use PredicateBuilder (LINQKit) for building dynamic OR conditions at runtime
  • Parentheses control precedence: && binds tighter than ||

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.