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 5969427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:13:44+00:00 2026-05-22T20:13:44+00:00

I have this project set up with EF4 and I’m using LINQ to Entities

  • 0

I have this project set up with EF4 and I’m using LINQ to Entities to form queries.

I’m having some trouble with a query that involves lots of conditional where clauses and several left outer joins.

I have partially solved the conditional where clauses with the following extension method (which I found here).

public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool condition, Expression<Func<TSource, bool>> predicate)
    {
        if (condition)
            return source.Where(predicate);
        else
            return source;
    }

The problem I have now is that LINQ to Entities doesn’t recognize the extension method when I use it on the joined tables.

Here’s a part of the query:

from p in context.Entities.OfType<Patient>()
    .WhereIf(!string.IsNullOrEmpty(name), p => p.Name.Contains(name))
from c in context.Contacts
    .Where(cp => cp.EntityID == p.EntityId).DefaultIfEmpty()
    .WhereIf(timestamp != null, c => c.Timestamp > timestamp)

I say partially solved because it works just fine the first time (patient name) in this query, but the second time (timestamp) it gives me this error:

LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[x.Contact] WhereIf[Contact](System.Linq.IQueryable`1[x.Contact], Boolean, System.Linq.Expressions.Expression`1[System.Func`2[x.Contact,System.Boolean]])' method, and this method cannot be translated into a store expression.

So, I’m wondering if anyone know how I can fix this?

  • 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-22T20:13:45+00:00Added an answer on May 22, 2026 at 8:13 pm

    The problem is that EF is unable to recognize WhereIf inside a SelectMany. Rewriting your query into method syntax yields something like this (query is incomplete):

    context.Entities.OfType<Patient>()
      .WhereIf(!string.IsNullOrEmpty(name), p => p.Name.Contains(name))
      .SelectMany(
        p => context.Contacts
          .Where(cp => cp.EntityID == p.EntityId).DefaultIfEmpty()
          .WhereIf(timestamp != null, c => c.Timestamp > timestamp),
        (p, c) => new { p, c }
      )
    

    The problem is the last WhereIf. You should be able to move it outside the SelectMany:

    context.Entities.OfType<Patient>()
      .WhereIf(!string.IsNullOrEmpty(name), p => p.Name.Contains(name))
      .SelectMany(
        p => context.Contacts
          .Where(cp => cp.EntityID == p.EntityId).DefaultIfEmpty(),
        (p, c) => new { p, c }
      )
      .WhereIf(timestamp != null, x => x.c.Timestamp > timestamp)
    

    You can inspect the resulting SQL to see if you get what you actually want by casting the query into ObjectQuery and calling the method ToTraceString:

    var query = context.Entities.OfType<Patient>() ...
    Console.WriteLine(((ObjectQuery) query).ToTraceString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this project in PHP where I have some include files next
I have this task for the project with 4 nested subprojects using Maven: For
We're about to set off our first larger WCF project, and we're having some
I have this project that has a set price for a certain amount of
I am currently working on a project. In this project I have set of
I am using POCO classes on an EF4 CTP5 project and I am having
I have just set up a new Eclipse Java project. In this project I
If I have a folder structure set up like this: ~/Projects emacs package1 package1-helpers
I have this project that it's due in a few hours and I still
Okay so, I have this project structure: package A.B class SuperClass (this class is

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.