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;
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- Linq to SQL - Many to Many Predicates
- source code for LINQ 101 samples
- how to write a Linq query with a EF code first Many to Many relationship
- LINQ Intersect Query for Many to Many relationship using .NET4 MVC4 Code First
- c# Linq or code to extract groups from a single list of source data
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- Entity-framework code is slow when using Include() many times
- How are people unit testing code that uses Linq to SQL
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- LINQ to entities - Building where clauses to test collections within a many to many relationship
- Syntax to execute code block inside Linq query?
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Is there a good source that gives an overview of linq optimizations?
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ WHERE method alters source collection
- Where Predicates in LINQ
- How to Select All with a One to Many Relationship Using Linq
- Where can I view LINQ source code?
- Suggestions for designing complex LINQ code
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Left outer join using LINQ -- understanding the code
- How to pass LinQ Expressions from F# to C# code
- Querying single table and merge result for same DateTime
- Where with IN operator using Linq on NHibernate
- Deploy a C# + Linq console application?
- LINQ to Objects filtering
- DbFunctions.TruncateTime LINQ equivalent in EF CORE
- Select with list among the variables
- Linq to replace dataTable values
- Modify the expression tree of IQueryable.Include() to add condition to the join
- SQL return consecutive records
- How to handle linq statement returning exception when no file found
- object with the same key already exists in the ObjectStateManager
- Sort by any of 3 time fields
- retrieving nested collections with LINQ to SQL
- KeyValuePair deconstruction in ToDictionary
- LINQ update table replacing NULL with EmptyString 1 million records
- Retrieving entities with related tables c# using REST API2
- Linq To Sql Multiple Where Search In Foreach Loop
- From SQL to LINQ
- C# Entity Framework - Sort View when reading from it
- Linq - Search table records contains any letter of search string