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 Query
- Linq to SQL - Many to Many Predicates
- How are people unit testing code that uses Linq to SQL
- LINQ Source Code Available
- How to code the partial extensions that Linq to SQL autogenerates?
- Determine the source DataContext for a Linq to Sql query
- How to get SQL query into LINQ form in C# code
- Many to Many relationship using SQL and Linq (entity framework/to entities)
- creating Linq to sqlite dbml from DbLinq source code
- Does LINQ convert code to SQL queries
- How do i convert this linq code to inline sql
- Reuse Linq to SQL code with entityframework
- Linq to SQL one to many relationships
- Identify source of linq to sql query
- Linq to sql as object data source - designer problem with partial classes
- SQL subquery result in LINQ and Entity Framework Code First
- Accessing SQL Server time in code with LINQ
- Linq to sql query: how to prevent duplication of code
- Linq to sql - Join 2 tables, select 1 row from right table with a 1 to many relationship
- convert linq to object query to sql query (no linq to sql code or datacontext)
- Linq union all equivalent of sql code
- LEFT LINQ TO SQL C# JOIN on many to many table
- LINQ to SQL select exact matching record from many to many table
- Linq to Sql Many to Many relationships
- LINQ vs sql - bringing back too many rows
- Translating Linq Code Block into SQL
- Linq to Sql : how to get data - one to many
- source code for LINQ 101 samples
- What SQL query or LINQ code would get the following data?
- C# LINQ to SQL - one to many relationship
- how to write a Linq query with a EF code first Many to Many relationship
More Query from same tag
- An error occurred while starting a transaction on the provider connection. See the inner exception for details
- C# Pass entity property to a function to use in LINQ
- Order by nullable objects at end with linq dynamic core
- C# How to cast List<object[]> into List<customClass>
- Why is it deleting from other variable while I am Removing from different variable in C#.net?
- Pull out Comma Seperated String from EntitySet<T>
- Limit collection by enum using lambda
- Creating a LINQ select from multiple tables
- C#, LINQ a generic sort method to sort a list by object properties and nested properties
- Linq how to Extend\Override existing toList() Method
- Localisation/I18n of database data in LINQ to SQL
- Angular2 linq style property accessor
- C# Sorting a Linq List<XElement> by Date node comprised of Wed, 30 Jul 2014 12:00:00 -0700
- Intersect Anonymous Types with Lambda Comparer
- Include Entities to Child Collection With Filter Linq EF5
- using c# generics extension methods to operate on a class and not a collection
- Complex Json type Querying
- Is there an easy way to append lambdas and reuse the lambda name in order to create my Linq where condition?
- Return all columns in anonymous linq join
- Enum with default typecast? is that possible?
- Removing values from a generic list
- Do multiple DbContext generate a deadlock?
- C# XML LINQ Syntax on Attributes
- Many to Many EF LINQ
- Filtering 2 lists in a performant way
- C# Core 2.0 - get data from one model based on data in another model?
- Will Microsoft ever make all collections useable by LINQ?
- Binding Linq.EntityRef to Gridview
- Sequence contains no elements error with linq
- one to zero or one relationship in entity framework