0

I have a string array of items & a list of database objects and I want to do a select query out of it

List<string> target_terms = new List<string> { "car", "mechanic" }

if (isNotExactPhrase)
{
    List<int> _tagIds = (from item in dbContext.Tags
                    where target_terms.Any(w => item.TagName.Contains(w))
                    select item.TagId).ToList();
}

I want all tags with names in the array

I have to use 2 options I want to check Tagname contains any of the keyword & If search is for exact phrase then I want to check any of the Tagname == any of the keyword

But for this query I am getting error

Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.

How to solve this?

2

3 Answers 3

2

Try this query

var _tagIds = (from item in dbContext.Tags
                    where keywords.contains(item.TagName)
                    select item.TagId).ToList();

There's no direct equivalent, but there are some methods work similarly, depending on the pattern.

string.Contains("pattern") is equivalent to LIKE '%pattern%'

string.StartsWith("pattern") is equivalent to LIKE 'pattern%'

string.EndsWith("pattern") is equivalent to LIKE '%pattern'

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

4 Comments

This one worked but when i try to use another Linq Qury with this _tagIds its showing the same error
Now check what is return.
How can i do the second case . Instead of using contains how can i check with == . Since if the user search for "car" and if there is a tag exists with word "carmel" that item will also be included
The Author tried to compare tags by substring. That is not the answer. (Author's Case: tag = "super_code", keyword = "er_co" ... your query will not return that as a result).
1

After hitting this problem a multitude of times, I created a nuget package that solves this common issue.

Using NinjaNye.SearchExtensions you can do the following:

var _tagIds = dbContext.Tags.Search(t => t.TagName)
                            .Containing(keywords)
                            .Select(t => t.TagId)
                            .ToList();

If you needed to you could also search against multiple properties and return records where a match was found in either property.

var _tagIds = dbContext.Tags.Search(t => t.TagName, t => t.Description)
                            .Containing(keywords)
                            .Select(t => t.TagId)
                            .ToList();

Hope this helps

Comments

-1

Try replacing Any with contains

List<int> _tagIds = (from item in dbContext.Tags
                    where target_terms.Contains(w => item.TagName.Contains(w))
                    select item.TagId).ToList();

1 Comment

I think Second contains should not be there if you are using target_terms.contains

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.