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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:03:47+00:00 2026-06-04T09:03:47+00:00

In linq to Entities we needed a method that works like sql like. We

  • 0

In linq to Entities we needed a method that works like “sql like”. We have implemented our own extension method to IQueryable, because contains method not work for us because doesn’t accept patterns like ‘%a%b%’

The created code is:

private const char WildcardCharacter = '%';

public static IQueryable<TSource> WhereLike<TSource>(this IQueryable<TSource> _source, Expression<Func<TSource, string>> _valueSelector, string _textSearch)
{
    if (_valueSelector == null)
    {
        throw new ArgumentNullException("valueSelector");
    }

        return _source.Where(BuildLikeExpressionWithWildcards(_valueSelector, _textSearch));
}

private static Expression<Func<TSource, bool>> BuildLikeExpressionWithWildcards<TSource>(Expression<Func<TSource, string>> _valueSelector, string _textToSearch)
{
    var method = GetPatIndexMethod();

    var body = Expression.Call(method, Expression.Constant(WildcardCharacter + _textToSearch + WildcardCharacter), _valueSelector.Body);

    var parameter = _valueSelector.Parameters.Single();
    UnaryExpression expressionConvert = Expression.Convert(Expression.Constant(0), typeof(int?));
    return Expression.Lambda<Func<TSource, bool>> (Expression.GreaterThan(body, expressionConvert), parameter);
}

private static MethodInfo GetPatIndexMethod()
{
    var methodName = "PatIndex";

    Type stringType = typeof(SqlFunctions);
    return stringType.GetMethod(methodName);
}

This works correctly and the code is executed entirely in SqlServer, but now we would use this extension method inside where clause as:

myDBContext.MyObject.Where(o => o.Description.Like(patternToSearch) || o.Title.Like(patterToSerch));

The problem is that the methods used in the where clause have to return a bool result if is it used with operators like ‘||’ , and I don’t know how to make that the code I have created returns a bool and keep the code executed in sqlserver. I suppose that I have to convert the returned Expression by BuildLinqExpression method to bool, but I don’t know how to do it

To sum up! First of all it’s possible to create our own extensiond methods in Linq to Entities that execute the code in SqlServer? if this is possible how I have to do it?

Thanks for your help.

  • 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-06-04T09:03:48+00:00Added an answer on June 4, 2026 at 9:03 am

    No, you cannot educate EF to process your custom extension methods, even though you have code that builds expressions that would be usable to EF.

    Either:

    • use the SqlFunctions methods directly in your EF Where expression
    • use an ExpressionVisitor to combine multiple expressions into a composite (OrElse / AndAlso) expression (note this won’t help you have code like you want, but it will let you use your two methods and perform a || on them – it will look complex, though)

    The first is simpler and clearer.

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

Sidebar

Related Questions

I have a IQueryable<SomePOCO> (a LINQ-Entities query, if that matters): public class SomePOCO {
When linq to entities was released, everybody say that linq to sql is dead.
I have a Linq to Entities query like this one: var results = from
I have a Linq-to-Entities query that is not complicated but requires an .include and/or
Using Linq to Entities, I have an IQueryable<T> and want to add another WHERE
I'm new to linq and linq to entities so I might have gone wrong
I have a simple LINQ-to-Entities query as below: var BillingNumbers = from o in
I'm new to linq-to-sql (and sql for that matter), and I've started to gather
I'm using linq to entities(EF). I have a constructor which takes 4 string parameters.
I am struggling linq to entities left outer join. I have two entities (tables):

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.