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 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

Ask A Question

Stats

  • Questions 317k
  • Answers 317k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The reason you're seeing only one property in the object… May 13, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer I suggest you put the "select" code into extension method… May 13, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer The analytic features are defined as part of the ANSI… May 13, 2026 at 11:46 pm

Related Questions

I have a Silveright app that allows users to specify filters on a few
What I need is such a simple, easy query, it blows me away how
We are developing an ASP.NET MVC application, and are now building the repository/service classes.
I want to find a section of xml based on a criteria so I’m
I'm trying to write my own toy My Toy Language -> MSIL compiler in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.