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

  • SEARCH
  • Home
  • 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 644791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:25:54+00:00 2026-05-13T21:25:54+00:00

Passing in an Expression to a Linq query behaves differently depending on syntax used,

  • 0

Passing in an Expression to a Linq query behaves differently depending on syntax used, and I wonder why this is the case.

Let’s say I have this very generic function

private IEnumerable<Company> 
    GetCompanies(Expression<Func<Company, bool>> whereClause)

The following implementation works as expected

private IEnumerable<Company> 
    GetCompanies(Expression<Func<Company, bool>> whereClause)
{
    return (from c in _ctx.Companies.Where(whereClause) select c);
}

But this next implementation does not compile
(Delegate ‘System.Func’ does not take 1 arguments)

private IEnumerable<Company> 
    GetCompanies(Expression<Func<Company, bool>> whereClause)
{
    return (from c in _ctx.Companies where whereClause select c);
}

Obviously I can just use the first syntax, but I was just wondering why the compiler does not treat the where keyword the same as the Where extension?

Thanks,
Thomas

  • 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-13T21:25:55+00:00Added an answer on May 13, 2026 at 9:25 pm

    The syntax for a query expression involving a where clause is (simplifying the complete grammar)

    from identifier in expression where boolean-expression select expression
    

    whereClause is not a boolean expression. To recitify this, you have to say

    from c in _ctx.Companies where whereClause.Compile()(c) select c;
    

    Note that if whereClause were a Func<Company, bool> you could get away with

    from c in _ctx.Companies where whereClause(c) select c;
    

    Note that

    from x in e where f
    

    is translated mechanically by the compiler into

    (from x in e).Where(x => f)
    

    I say mechanically because it performs this translation without doing any semantic analysis to check validity of the method calls etc. That stage comes later after all query expressions have been translated to LINQ method-invocation expressions.

    In particular,

    from c in _ctx.Companies where whereClause select c
    

    is translated to

    _ctx.Companies.Where(c => whereClause).Select(c)
    

    which is clearly nonsensical.

    The reason that

    from c in _ctx.Companies.Where(whereClause) select c
    

    is legit is because IEnumerable<Company>.Where has an overload accepting a Func<Company, bool> and there is an implicit conversion from an Expression<Func<Company, bool>> to a Func<Company, bool>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any suggestions (or a regular expression) for parsing the HTTP Accept
I have an expression tree I have created by parsing an Xml using the
I keep getting an exception about Linq to Entities not supporting certaion query expressions
I am playing about with the IQueryProvider.Execute command and am passing in an expression
I am parsing an Expression Tree. Given a NodeType of ExpressionType.MemberAccess, how do I
Can someone provide a regular expression for parsing name/value pairs from a string? The
I am looking to create an expression tree by parsing xml using C#. The
Passing an undimensioned array to the VB6's Ubound function will cause an error, so
I'm passing raw HTTP requests to an apache server (received by PHP). The request
When you're passing variables through your site using GET requests, do you validate (regular

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.