score:3
I believe you can find notes that have the relevant tags in a single LINQ expression:
IQueryable<Note> query = ... // top part of query
query = query.Where(note => searchTags.All(st =>
note.Tags.Any(notetag => notetag.Id == st.Id)));
Unfortunately there is no “fluent syntax” equivalent for All
and Any
, so the best you can do there is
query = from note in query
where searchTags.All(st =>
note.Tags.Any(notetag => notetag.Id == st.Id))
select note;
which is not that much better either.
score:0
For starters see my comment; I suspect the query is wrong anyway! I would simplifiy it, by simply enforcing separately that each tag exists:
IQueryable<Note> query = ... // top part of query
foreach(var tagId in searchTagIds) {
var tmpId = tagId; // modified closures...
query = query.Where(note => note.Tags.Any(t => t.Id == tmpId));
}
This should have the net effect of enforcing all the tags specified are present and accounted for.
score:0
Timwi's solution works in most dialects of LINQ, but not in Linq to Entities. I did find a single-statement LINQ query that works, courtesy of ReSharper. Basically, I wrote a foreach block to do the search, and ReSharper offered to convert the block to a LINQ statement--I had no idea it could do this.
I let ReSharper perform the conversion, and here is what it gave me:
return searchTags.Aggregate<Tag, IQueryable<Note>>(DataStore.ObjectContext.Notes, (current, tag) => current.Where(n => n.Tags.Any(t => t.Id == tag.Id)).OrderBy(n => n.Title));
I read my Notes collection from a database, using Entity Framework 4. DataStore is the custom class I use to manage my EF4 connection; it holds the EF4 ObjectContext as a property.
Source: stackoverflow.com
Related Query
- LINQ Query to find all tags?
- Find All Rows in DataTable Where Column Value is NOT Unique Using Linq Query
- LINQ query to find if all items in list 2 are contained in list 1
- LINQ to XML Query to find all child values where parent element name like X C#
- LINQ query to find if items in a list are contained in another list
- LINQ - Find all items in one list that aren't in another list
- Linq find all with certain type
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- LINQ Lambda - Find all ID's in one list that don't exist in another list
- Find all child controls of specific type using Enumerable.OfType<T>() or LINQ
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- How to use LINQ to find all combinations of n items from a set of numbers?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Where to find translated Linq to Entity query to Sql
- Using LINQ to find all keys from one collection that are not in another?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Query to check for a predicate in all columns in a table
- LINQ Source Code Available
- List of all LINQ query expression keywords?
- Linq query - find strings based upon first letter b/w two ranges
- LINQ to EF - Find records where string property of a child collection at least partially matches all records in a list of strings
- LINQ query on DataTable - Could not find an implementation of the query pattern
- C# Linq Find all indexes of item in List<int> within another List<int>
- linq query for tag system - search for multiple tags
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- How to construct IQueryable query using Linq when I just need count without reading all documents in Document-Db database?
- C# : LINQ query to list all empty properties of a class
- c# - Linq Query to retrieve all objects with a max value
More Query from same tag
- How to handle null values while parsing XML in C# .Net?
- twice reading xml file with linq
- How to do a simple table join in fluent Nhibernate 4
- How do I write a SQL Concat in Linq?
- Is using LinqToSql in a recursive method very bad for performance
- Tuples - Only parameterless constructors and initializers are supported in LINQ to Entities
- Problem using linq to concat two collections
- Orderby a string column in linq causes an error
- How to use LINQ to adjust result?
- How to merge rows using linq, which contains a boolean and a sub list?
- LINQ Query incredibly slow - why?
- How to Optimize below linq query
- Linq - How to aggregate the results of another query
- Linq where condition on datetime.ToString()
- Remove a Key from Dictionary by key name
- Inner join to get those not in table
- How to assign two different entities result set into single var variable using linq to sql c#
- looping through element of XDocument and getting specific attributes
- Linq query with several left join generate few query
- How to compare two tables in linq to sql?
- Linq Query Always returns False and Failed to Fetch Data
- Check if list contains item from other list in EntityFramework
- Invalid Linq query using Where
- Option to join objects in list with list from other db
- How to select multiple item from list with related condition
- Trying to filter only digits in string array using LINQ
- LINQ: How to Sum over Grouping using method style linq?
- get attribute value from xsd node using LINQ
- How can I merge time entries into a single row for a weekly report?
- How to compare all items in the lists are equal?