score:3

Accepted answer

You can do it in one query if you just start with the ProductTags table. You'll probably also need a Distinct to avoid duplicate products matching multiple tags.

var products = (from pt in db.ProductTags
                where pt.Tag.Name.Contains( tagSearchQuery )
                select pt.Product).Distinct();

or here's another way:

var products = from p in db.Products
                  from pt in p.ProductTags
                  where pt.Tag.Name.Contains( tagSearchQuery )
                  select p

score:0

IQueryable<Tag> tags =
  from t in db.Tags
  where t.Name.Contains( tagSearchQuery )
  select t;

IQueryable<Product> products =
  from p in db.Products
  where p.ProductTags.Any(pt => tags.Contains(pt.Tag))
  select p;

OR

IQueryable<Product> products =
  from p in db.Products
  from pt in p.ProductTags
  let t = pt.Tag
  where t.Name.Contains( tagSearchFragment )
  group t by p into g
  select g.Key;

Related Articles