Q: Linq select where contains I have 2 objects (tried to make it work a bit like SO) with a many to many relationship that I would like to filter one collection of them based on the other. here are my object:
public class Code
{
    public int CodeId { get; set; }
    public string CodeName { get; set; }
    public string CodeData { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int TagId { get; set; }
    public string Name { get; set; }
    public string Comments { get; set; }

    public virtual ICollection<Code> Codes { get; set; }
}

So if I have a collection of Tags that I want to filter upon I want to return a collection of Codes that their tags includes at least one of the selected tags but that there's no Tag in the selected Tags that doesn't exist in Code.

I tried using Linq for it but eventually got to this non-linq solution:
public IEnumerable<Code> GetCodes()
    {
        //A collection to filter upon
        var SelectedTags = new List<Tag>();

        //the collction to return
        var FoundCodes = new List<Code>();

        foreach (Code code in GetAllCodes())
        {
            foreach (var tags in code.Tags)
            {
                if (SelectedTags.Contains(tags))
                {
                    if (!FoundCodes.Contains(code))
                    {
                        FoundCodes.Add(code);
                    }
                    continue;
                }
            }
            foreach (var selectedTags in SelectedTags)
            {
                if (!code.Tags.Contains(selectedTags))
                {
                    FoundCodes.Remove(code);
                }
            }
        }

        return FoundCodes;
    }

So my question is how do I do it with Linq?

A: erm, how about.
var foundCodes = GetAllCodes()
                   .Where(code => code.Tags.All(t => SelectedTags.Contains(t)))
                   .Distinct()
                   .ToList();


If GetAllCodes() returns a distinct result you can omit the Distinct call.

following on from @Nuffin's comment,
you could try,
SelectedTags = SelectedTags.OrderBy(t => t).ToList();

var foundCodes = GetAllCodes()
                   .Where(code =>
                       code.Tags
                           .OrderBy(t => t)
                           .SequenceEqual(SelectTags))
                   .Distinct()
                   .ToList();

but, I suspect your Query Provider will be unable to translate SequenceEqual into a statement.
In this case, use @Nuffin's answer.

A: This simple query should do the trick (it finds all codes which has all selected tags, and no other tags): (Note that while this code exactly mimicks the OP's implementation's behavior, it's not the desired result. See further below for the fixed version)
public IEnumerable<Code> GetCodes()
{
    var SelectedTags = new List<Tag>(); /* fill this however */
    return from code in GetAllCodes() /* get all codes*/
           where SelectedTags.All(code.Tags.Contains) /* filter out codes with nonmatching tags */
           where code.Tags.All(SelectedTags.Contains) /* filter out codes with missing tags*/
           select code; /* return the remaining codes */
}

Note that depending on the number of different tags this could be slow without use of collections which are optimized for searching (although still a bit faster than your solution).
Edit: To get the result illustrated on your image (get all codes with all selected tags, regardless of other tags the code has), just omit the second where clause:
public IEnumerable<Code> GetCodes()
{
    var SelectedTags = new List<Tag>(); /* fill this however */
    return from code in GetAllCodes() /* get all codes*/
           where SelectedTags.All(code.Tags.Contains) /* filter out codes with nonmatching tags */
           select code; /* return the remaining codes */
}


A: Maybe you can shorten this so:
public IEnumerable<Code> GetCodes()
{
  //A collection to filter upon
  var SelectedTags = new List<Tag>();

  //the collction to return
  var FoundCodes = new List<Code>();

  foreach (var code in GetAllCodes())
  {
    foreach (var tags in code.Tags.Where(tags => SelectedTags.Contains(tags)))
    {
      if (!FoundCodes.Contains(code))
      {
        FoundCodes.Add(code);
      }
      continue;
    }
    foreach (var selectedTags in SelectedTags.Where(selectedTags => !code.Tags.Contains(selectedTags)))
    {
      FoundCodes.Remove(code);
    }
  }

  return FoundCodes;
}

Maybe you could post GetAllCodes() ... The Problem is that because of the to Loops inside the foreach i dont know how to shorten it more into a linq method chain.
