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 216167

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:32:20+00:00 2026-05-11T18:32:20+00:00

The All method is supposed to evaluate the argument against all elements in the

  • 0

The All method is supposed to evaluate the argument against all elements in the list. It works OK in regular Linq but when I try to use it with EF it throws an error (“Unable to create a constant value of type ‘Closure type’. Only primitive types (for instance Int32, String and Guid) are supported in this context. “)

Example:

var myList = from person in entities.People
             where searchList.All(arg => arg == arg).ToList();

(arg == arg here is just to illustrate my question)

In my scenario, searchList is a List containing search items, such as “John”, “Accounting”, “75”. In my EF query I want to retrieve all records in People which John, Accounting and 75 appear in some specified searchable fields. A more realistic example would be something like this:

where SearchList.All((person.FirstName + " " + person.LastName + " " + person.DepartmentName + " " + person.Phone).Contains)

The second example also works OK with Linq, in memory, but EF doesn’t like it.

Please help! What can I do to make it work?

This is a more specific question derived from another question of mine.

Sample code:

IEnumerable<string> searchList = ParseSearchText(searchText); //search text is broken into search tokens - each token is an element in searchList. For instance "John", "Sales", "654"

var peopleQuery = from person in entities.vSearchPeople
where upperSearchList.All((person.FirstName + " " + person.Lastname + " " + person.Phone).ToUpperInvariant().Contains)
select person;
  • 0 0 Answers
  • 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-11T18:32:21+00:00Added an answer on May 11, 2026 at 6:32 pm

    The Entity Framework does not support all queries. This becomes obvious if you think of something like the following

    dataContext.Persons.Where(person => MyMethod(person));
    

    with MyMethod() returning a Boolean value. The method might do everything and you cannot translate everything into SQL. The solution is to get all entities into local memory using ToList() and then use LINQ to Object.

    dataContext.Persons.ToList().Where(person => MyMethod(person));
    

    It depends on your actual query if it can be rewritten, so that it can be transformed into SQL by the Entity Framework or if you have to do the query in local memory using LINQ to Object.

    The exception you mentioned sound like you are trying something like the following.

    Company company = datacontext.Companies.Where(company.Name == "ACME").Single();
    
    dataContext.Employees.Where(employee => employee.Company == company);
    

    LINQ to Entity does not support expressions containing entities, hence the comparison of the Company entities is not valid. In this case you can rewrite it as follows.

    dataContext.Employees.Where(employee => employee.Company.Id == company.Id);
    

    This compares only the ids – a primitive type like a integer or a GUID – and this can be transformed into SQL.

    Example for search word by word (see also the comments)

    IQueryable<People> result = entities.People;
    
    foreach (String item in searchList)
    {
        // This copy is important in order not to modify the closure.
        String itemCopy = item;
    
        result = result.Where(p =>
            p.FirstName.ToUpper().Contains(itemCopy) ||
            p.LastName.ToUpper().Contains(itemCopy) ||
            p.Phone.ToUpper().Contains(itemCopy));
    }
    

    This will construct the query word by word. Noted that the Entity Framework recognizes ToUpper(), ToLower(), and Contains() (and some more) – so I was to strict when I said that the Entity Framework does not recognize method calls. It does, but not many and not ToUpperInvariant() and ToLowerInvariant(). Further this query translates into CHARINDEX() function calls using the collation of the column, hence the search can be case insensitive without explicit ToUpper() or ToLower() calls.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, you can nest a datagrid inside another datagrid's rowtemplate.… May 11, 2026 at 9:48 pm
  • Editorial Team
    Editorial Team added an answer Updated: This solution was updated following a follow-up question in… May 11, 2026 at 9:48 pm
  • Editorial Team
    Editorial Team added an answer Your second option with DTOs is my preferred way. Your… May 11, 2026 at 9:48 pm

Related Questions

I wrote this simple test code, by adapting a piece from a book, to
I'm trying to fetch the last inserted row Id of a Sqlite DB in
I copied the log4net samples to implement a custom log level, AUDIT. I defined
This question is a more special case of the problem described (and solved) in
(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass

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.