0

In the Dat Access Layer of an ASP.NET Core MVC project, I need to get a list of "news" matching with any tags passed as parameter, something like:

public List<News> GetNewsByTags(List<String> tagsList)
{
    return database.News.Where(news => news.Tags.ContainsAnyElementOf(tagsList));
}

but I didn't succeed to make something like this work. For information, this is my request for only one tag:

public List<News> GetNewsByTag(String tag)
{
    return database.News.Where(news => news.Tags.Contains(tag)).ToList();
}
3
  • 1
    Is news.Tag a string or a list of strings? Commented Apr 11, 2018 at 14:53
  • news.Tags is a string build like that : "tag1;tag2;tag3" Commented Apr 11, 2018 at 14:55
  • Ok, that explains the error. Updated Commented Apr 11, 2018 at 14:56

3 Answers 3

2

Since your Tags is a single string with semicolons, you should do it in two phases: first, query on the SQL side with Contains, then filter what comes back to avoid false positives:

var preliminary = database.News
    .Where(news => tagsList.Any(t => news.Tags.Contains(t)))
    .ToList();
var tagSet = new HashSet<string>(tagList);
return preliminary.Select(news => new {
    News = news
,   MatchCount = news.Tags.Split(';').Count(t => tagSet.Contains(t))
}).Where(p => p.MatchCount > 0)
.OrderByDescending(p => p.MatchCount)
.Select(p => p.News);
Sign up to request clarification or add additional context in comments.

Comments

0

What about this ?

var allInCollection = news.Tags.Any(x => tagsList.Contains(x));

Comments

0

So you need to check that any of the news tag appears in the list of tags given. That translates quite literally to LINQ:

database.News.Where(news =>
    tagsList.Any(tag => news.Tags.Contains(tag))
)

2 Comments

I got an error on the news.Tags.Split(';') : An expression tree may not contain a call or invocation that uses optional arguments
@Alexis, just figured it can be rewritten in other way, see updated version

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.