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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:50:39+00:00 2026-05-24T16:50:39+00:00

Lets say that I have a particular way of deciding whether some strings match,

  • 0

Lets say that I have a particular way of deciding whether some strings “match”, like this:

public bool stringsMatch(string searchFor, string searchIn)
{
  if (string.IsNullOrEmpty(searchFor))
  {
    return true;
  }

  return searchIn != null &&
    (searchIn.Trim().ToLower().StartsWith(searchFor.Trim().ToLower()) ||
     searchIn.Contains(" " + searchFor));
}

I would like to pull matches out of a database using Linq To Entities and this helper. However, when I try this:

IQueryable<Blah> blahs = query.Where(b => stringsMatch(searchText, b.Name);

I get “LINQ to Entities does not recognize the method…”

If I re-write the code as:

IQueryable<Blah> blahs = query.Where(b =>
      string.IsNullOrEmpty(searchText) ||
      (b.Name != null &&
        (b.Name.Trim().ToLower().StartsWith(searchText.Trim().ToLower()) ||
         b.Name.Contains(" " + searchText)));

Which is logically equivalent, then things work fine. The problem is that the code isn’t as readable, and I have to re-write it for each different entity I want to match.

As far as I can tell from questions like this one, what I want to do is impossible at the moment, but I’m hoping that I’m missing something, am I?

  • 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-24T16:50:41+00:00Added an answer on May 24, 2026 at 4:50 pm

    Using a freely available library called LINQKit (as mentioned by @Eranga) this task becomes reasonable. Using LINQKit the code I have now looks like:

    protected Expression<Func<T, bool>> stringsMatch(string searchFor, Expression<Func<T, string>> searchIn)
    {
      if (string.IsNullOrEmpty(searchFor))
      {
        return e => true;
      }
    
      return
        e =>
        (searchIn.Invoke(e) != null &&
          (searchIn.Invoke(e).Trim().ToLower().StartsWith(searchFor.Trim().ToLower()) ||
           searchIn.Invoke(e).Contains(" " + searchFor)));
    }
    

    And needs to be called like this (note the AsExpandable() call)

    IQueryable<Blah> blahs = query().AsExpandable().Where(StringsMatch(searchText, b => b.Name));
    

    The magic parts are the searchIn.Invoke(e) calls and the use of AsExpandable() which adds a wrapper layer that allows them to work.

    The AsExpandable() bit is explained in detail by the original author here.

    Note that I’m still a bit hazy on some of the details of expressions, so please add a comment/edit this answer if it can be made better/shorter/clearer.

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

Sidebar

Related Questions

Lets say that we have a query like this one: SELECT *, (CUSTOM_EXPRESSION) as
Lets say that I have the following code: public class Shelter<A extends Animal, B
Lets say I have some simple classes for a twitter-like app: User Post A
We have links on particular pages, that are served dynamically. So lets say we
Lets say that i have 2 pages: index.php and service.php index.php sends an http-post
Lets say that you have a following simple application: <form action=helloServlet method=post> Give name:<input
Lets say that i have a duration of time e.g., 10 minutes . I
Lets say that you have a business object whose current state implies that there
Lets say that I have an executable and when it is started I want
Lets say that I have only DHT (distributed hash table) implemented (in Python), and

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.