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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:00:12+00:00 2026-05-16T08:00:12+00:00

I have a two objects as follows: public class Item { public int ItemId

  • 0

I have a two objects as follows:

public class Item
{
    public int ItemId {get;set;}
    public string ItemName {get;set;}
    public List<Tag> ItemTags {get;set;}
    public DateTime DateCreated {get;set;}
}

public class Tag
{
    public int TagId {get;set;}
    public string TagName {get;set;}
}

These are LINQ-to-SQL objects, so the ItemTags will be an EntitySet.

I am trying to perform a search query where a user can provide a comma delimited list of tags as a search filter.

How do I filter my list of items to those which contains all of the tags in the comma delimited list.

EDIT2

e.g.
Item1 has tags of Apple, Banana, Orange
Item2 has tags of Banana, Orange
Item3 has tags of Pineapple, Orange
If the tag filter is "Banana, Orange" I need the results to be Item1 and Item2.

/EDIT2

This is what I have tried thus far:

string tags = "Manchester United,European Cup,2008";
List<string> tagsList = tags.Trim().ToLower()
    .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Distinct(StringComparer.CurrentCultureIgnoreCase)
    .ToList();

List<Item> itemList = ItemRepository.FetchAll();

var query = itemList
    .OrderByDescending(p => p.DateCreated)
    .ToList();

if (tagsList.Count() > 0)
{
    query = query
        .Where(p => p.ItemTags
            .Select(q => q.TagName.ToLower())
            .All(r => tagsList.Contains(r)))
        .ToList();
}

However, this doesn’t seem to work. Any ideas on what I am doing wrong please?

EDIT1: tags are trimmed and are ‘lowercased’.

  • 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-16T08:00:12+00:00Added an answer on May 16, 2026 at 8:00 am

    That because you’re puting the tags from the items to lowercase, but not the searched tags.

    With this modification it should work:

    List<string> tagsList = tags
        .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
        .Select(s => s.ToLower())
        .Distinct()
        .ToList();
    

    EDIT: OK, I see what the problem is: you’re doing it backwards. You’re searching for items that have only the tags that you’re looking for.

    Try that instead:

    query = 
        (from item in query
         let itemTags = p.ItemTags.Select(it => it.TagName.ToLower())
         where tags.All(t => itemTags.Contains(t))
         select item).ToList();
    

    UPDATE: here’s a version with the lambda syntax. It’s pretty ugly because of the temporary anonymous type, but that’s how the let clause translates to lambda…

    query =
        query.Select(item => new { item, itemTags = item.ItemTags.Select(it => it.TagName.ToLower()) })
             .Where(x => tagsList.All(t => x.itemTags.Contains(t)))
             .Select(x => x.item)
             .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two objects, one being the list of items, and the other
I have two lists of objects. Each list is already sorted by a property
I have two objects, let's call them Input and Output Input has properties Input_ID
If I have two objects on the heap referring to each other but they
Ok, NHibernate question here. I have two objects that I would like to map
I have two DateTime objects: StartDate and EndDate . I want to make sure
I have two LINQ objects which have exactly the same columns and I would
I have two JSON objects with the same structure and I want to concat
Example: I have two shared objects (same should apply to .dlls). The first shared
I have two lists of custom objects and want to update a field for

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.