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 Articles
- LINQ query to find if items in a list are contained in another list
- 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()
- Where to find translated Linq to Entity query to Sql
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Source Code Available
- Linq query - find strings based upon first letter b/w two ranges
- LINQ query on DataTable - Could not find an implementation of the query pattern
- 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 dynamic add filters to a LINQ query against an Odata Source in C#
- using linq query to find value that differs from previously found value
- Avoid extra loop and could not find implementation of query pattern for source type int Select not found
- C# Linq query help removing foreach loops creating cleaner code
- Use a linq query as microsoft local report Data Source (WinForms)
- Determine the source DataContext for a Linq to Sql query
- LINQ query returns old results when source list is re-initialized
- Can't find the appropriate operators for a Linq query
- How to get SQL query into LINQ form in C# code
- linq query to find distinct combinations of list items taking 2 at a time c#
- Could not find an implementation of the query pattern for source type
- How can I code a Linq query to do an upward Include?
- Linq query to find duplicate objects based on multiple fields AND property is null
- Cannot find xml element using linq query in Portable Class Library
- creating Linq to sqlite dbml from DbLinq source code
- LINQ Query to Find the Maximum Mean for a Time Span
- How do I do a linq query to find fields in a dataset that are present in every record of a set?
- Identify source of linq to sql query
- Linq query to find date range
- Linq observable collection cast error
- linq selecting into custom object
- How to extend the ADO.NET entity designer?
- Automatically checking for NULL relationships with LINQ queries
- How to select list class properties on the base max value in each group?
- Using Linq to select subset from a Dictionary<string, int>?
- Linq - How to apply a where clause on the maximum element of a list
- Converting datatable into hierarchical data structure (JSON) using C#
- non executing linq causing memory allocation C#
- Select Linq statement needs to return 0 if either item is null
- Select from list where not appear in second list
- In C#, what is the best way to sort a list of objects by a string property and get correct order?
- FirstOrDefault() == null ? false : true;
- Linq CSV Import with line-numers (index)
- Concatenating objects with linq
- How to load DTO Within DTOs?
- C#/Linq: Apply a mapping function to each element in an IEnumerable?
- Join PropertyInfo[] with iEnumerable<T> where every PropertyInfo.Name equals any T.variableContent
- List View Sliding Window
- Get product of items in list C# linq