views:

46

answers:

2

I seem to be having trouble with this. I have a Task table with an ID, and a Tag table, that has a tag field and a foreign key constraint to the task table.

I want to be able to perform AND searches for tasks by tags. So for example, if I search for tasks with the tags "portability" and "testing", I don't want tasks that are tagged with "portability" and not "testing".

I tried the following syntax:

 var tasks = (from t in _context.KnowledgeBaseTasks
                     where t.KnowledgeBaseTaskTags.Any(x => tags.Contains(x.tag))
                     select KnowledgeBaseTaskViewModel.ConvertFromEntity(t)
                    ).ToList();

This of course does an OR search, not an AND search. I can't figure out how to actually switch this to be an AND search.

Edit I also need to be able to search for 2 out of X tags that a task contains. So if the task is tagged with "bugfix", "portability", "testing" and I search for "testing" and "portability", that task will still show up.

+3  A: 

Use All instead of Any; and in order to only select the KnowledgeBaseTasks, that has all the tags (but possibly more); reverse the expression:

var tasks = (from t in _context.KnowledgeBaseTasks
                 where tags.All(tag => t.KnowledgeBaseTaskTags.Contains(tag))
                 select KnowledgeBaseTaskViewModel.ConvertFromEntity(t)
                ).ToList();
driis
Sorry I should have been more clear. The problem with All is that if my task also has a "bugfix" tag, just searching for "portability" and "testing" tags won't bring it up
KallDrexx
@KallDrexx, See edited answer.
driis
The only issue with that is you can't do .Contains(string) on KnowledgeBaseTags, because it's expecting a KnowledgeBaseTag entity to be passed in, not a string. I tried using `where tags.All(tag => t.KnowledgeBaseTaskTags.Any(entity => entity.tag == tag))` but that gives an exception (Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.)
KallDrexx
+1  A: 

You want to do this

the LinqToSql might look like:

List<int> myTags = GetTagIds();
int tagCount = myTags.Count;

IQueryable<int> subquery =
  from tag in myDC.Tags
  where myTags.Contains(tag.TagId)
  group tag.TagId by tag.ContentId into g
  where g.Distinct().Count() == tagCount
  select g.Key;

IQueryable<Content> query = myDC.Contents
  .Where(c => subQuery.Contains(c.ContentId));

I haven't tested this and the Distinct bit might be off a little. Check the generated sql to be sure.

David B
This worked perfectly (Had to add a ToList() to the subquery and Select() call to the main query but otherwise it worked.
KallDrexx