4

I want to compare a sting field with a string array using LINQ, I’ve written the below extension method for comparing but it doesn’t work correctly.

 public static bool Contains(this string source, string[] keys)
    {
        foreach (var item in keys)
        {
            if (source.Contains(item))
                return true;
        }

        return false;
    }

And this is my query:

string[] keys = key.Split('+');
        var pages = context.Pages.Where(x => x.Title.Contains(keys));

The error that I’ve got is:

LINQ to Entities does not recognize the method 'Boolean Contains(System.String, System.String[])' method, and this method cannot be translated into a store expression.

3 Answers 3

7

You can't use your own custom methods within LINQ like this - but you may be able to get away with rewriting it using the normal LINQ operators:

string[] keys = key.Split('+');
var pages = context.Pages.Where(x => keys.Any(key => x.Title.Contains(key)));

If that doesn't work, I suspect it's basically infeasible in EF.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Jon Skeet it works :) but I like to know why my method doesn't work.
@Jon Skeet this doesn't work in EF Core, the expression doesn't get translated. Any ideas?
@Nexus: I'm afraid not. I don't have any experience with EF Core.
@JonSkeet No worries, thank you very much. If I remember correctly this works fine in EF6 but EF Core lacks some LINQ extensions apparently.
this no longer works in latest version .... trying to find when it broke and what the fix is..not wokring in ef core 5, im sure it was in 3.1 tho
4

The lambda expression that you write within Where is actually evaluated and translated to SQL by EF. For example when you write

db.Pages.Where(x => x.Title == "Hello")

EF knows to translate the resulting IQueryable<T> to something like:

SELECT ... FROM Pages WHERE title = 'Hello'

Similarly, it knows about some of the common methods and operators, for example:

db.Pages.Where(x => x.Contains("Hello"))

EF knows to translate String.Contains to:

SELECT ... FROM Pages WHERE title LIKE '%Hello%'

However, when you use your own custom methods with the expression, then EF does not know how to translate that custom method to SQL, which is what it complains about in the error message you saw.

Comments

1

LINQ to Entities does not recognize the method 'Boolean Contains(System.String, System.String[])' method, and this method cannot be translated into a store expression.

See your code is being translated to SQL , and if i am not wrong your error says 'Boolean Contains(System.String, This method cannot be translated , It means this particular section in unable to convert itself to SQL ,If you cast your string as BIT it may work . Well if someone feels i may be in wrong direction please let me know .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.