Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1071733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:45:11+00:00 2026-05-16T20:45:11+00:00

I’m trying to get the following SQL query to work in LINQ: Select id

  • 0

I’m trying to get the following SQL query to work in LINQ:

    Select id from table1 where id in (1,2) or canceledId in (1,2)

I’m using BuildContainsExpression to achieve the “IN” condition, but I can’t figure out how to implement the “or” condition.
My shot in the dark is as follows:

var identifiers = new List<int> {1,2};

var query = (from t in Context.Table1
             select t);

var query =
    query.Where(BuildContainsExpression<Table1, int>(t => t.Id, identifiers));

if (showCanceled)
{
   var expression = query.Where(BuildContainsExpression<Table1, int>(t => t.CanceledId.Value, identifiers)).Expression;
   Expression.Or(expression, transactionsQuery.Expression);
}

But I get the following exception:
The binary operator Or is not defined for the types ‘System.Linq.IQueryable1[Table1]' and 'System.Linq.IQueryable1[Table1]’..

Any ideas? -Am I in the right direction?

Thanks,
Nir.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:45:11+00:00Added an answer on May 16, 2026 at 8:45 pm

    You are appending your OR in the wrong place. What you are doing now is effectively something like this:

    (from t in Context.Table1
     where identifiers.Contains(t.Id)
     select t)
    OR
    (where identifiers.Contains(t.CanceledId))
    

    The second problem is that the BuildContainsExpression method you use, returns a lambda expression, something that looks like this:

    t => t.Id == 1 || t.Id == 2 || ...
    

    You can’t change this expression once it’s generated. However, that’s what you want because you’d like to have something like this:

    t => t.Id == 1 || t.Id == 2 || ... || t.CanceledId == 1 || t.CanceledId == 2 || ...
    

    You can’t simply take the body of this lambda expression and or it together with another expression because it depends on the parameter t.

    So what you can do is the following:

    // Overload of BuildContainsExpression.
    private static Expression<Func<T, bool>> BuildOtherContainsExpression<T>(
        ParameterExpression p, Expression field1, Expression field2, int[] values)
    {
        var eq1 = values.Select(v => Expression.Equal(field1, Expression.Constant(v)));
        var eq2 = values.Select(v => Expression.Equal(field2, Expression.Constant(v)));
    
        var body = eq1.Aggregate((acc, equal) => Expression.Or(acc, equal));
        body = eq2.Aggregate(body, (acc, equal) => Expression.Or(acc, equal));
        return Expression.Lambda<Func<T, bool>>(body, p);
    }
    
    // Create a parameter expression that represents something of type Table1.
    var parameter = Expression.Parameter(typeof(Table1), "t");
    
    // Create two field expressions that refer to a field of the parameter.
    var idField = Expression.Property(parameter, "Id");
    var canceledIdField = Expression.Property(parameter, "CanceledId");
    
    // And finally the call to this method.
    query.Where(BuildContainsExpression<Table1>(
        parameter, idField, canceledIdField, identifiers));
    

    Your if statement would now look like this:

    if (!showCanceled)
    {
        // Use original version of BuildContainsExpression.
    }
    else
    {
        // Create some expressions and use overloaded version of BuildContainsExpression.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.