score:0
what about the below. I haven't tried it but it gives you an idea about
var terms = "bob town".Split(' ');
var q = from m in db.Monument
where m.Name.Split(' ').Intersect(terms).Count() > 0 ||
m.Street.Split(' ').Intersect(terms).Count() > 0
select m;
Anyway I think for this kind of search you should think about using SQL Full-Text-search if you got a SQL db or ".net lucene" which fits really well in your context
score:0
I'm sure there is probably a way to do this with pure LINQ
, but I am not so sure if it is actually a good idea: IMHO opinion people sometimes tend to overcook their LINQ
queries for no particular reason other than LINQ
being very cool and all, ending up with queries that are very hard to understand by just looking at them.
Why don't you implement an extension method that takes Monument
entities and figures out if a certain string[]
matches your criteria?
That way your LINQ
expression couldn't get simpler:
var q = from m in db.Monument
where m.ContainsAllSearchTerms(terms)
select m; //readable and anyone understands right away what is going on here
Being ContainsAllSearchTerms(this Monument m, string[] terms)
the relevant extension method.
score:1
So, the actual problem is that Linq-To-SQL doesen't know how to convert the Terms
part of your Linq to a valid SQL statement. So, you have to rejig the query to help it out.
I assume that we would like to use the Contains
function to get a SQL statement that uses the SQL IN
operator. Here is what I suggest.
var terms = "bob town".Split(' ');
var q = from m in db.Monument
where
terms.Contains(m.Name)
||
terms.Contains(m.Street)
||
terms.Contains(m.Owner)
select m;
I haven't tested this but it looks like it should work and should be converted by Linq-To-SQL.
Source: stackoverflow.com
Related Articles
- Search query gives LINQ to SQL exception
- Linq help - sub query gives null pointer exception
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Why does LINQ query throw an exception when I attempt to get a count of a type
- Convert string to int in an Entity Framework linq query and handling the parsing exception
- Exception on Inner LINQ query when calling ToList()
- C# search query with linq
- Is there a good source that gives an overview of linq optimizations?
- Mongo Driver Linq Query throws Exception
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Strange null reference exception in LINQ query with EF
- Null reference exception being thrown in EF LINQ query if-clause
- LINQ query with two joins that worked in EF 6 gives error in EF 7
- LINQ Source Code Available
- InvalidOperationException: An exception was thrown while attempting to evaluate a LINQ query parameter expression
- 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?
- LINQ query null exception when Where returns 0 rows
- How can I write the following code more elegantly using LINQ query syntax?
- Why is LINQ Where search query faster on List<> compared to ConcurrentBag<>
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- Exception in a CRM LINQ query with joins. Attribute in second table doesn't exist
- use query linq in search with empty field
- C# Linq query help removing foreach loops creating cleaner code
- Create linq query to search for contact the same way smartphone will do so
- How to improve this LINQ query for search
- Use a linq query as microsoft local report Data Source (WinForms)
- Determine the source DataContext for a Linq to Sql query
- Timeout Exception in SQL Server using via LINQ Query
- Is possible to run a query with linq to search for a period of time?
- simplify for iteration to linq query
- LINQ (Unable to cast object of type 'System.Double' to type 'System.String'.)
- Can I use LINQ to generate random numbers reliably in C#?
- Unexpected behaviour when sorting a List<String>
- C# Entity LINQ returns wrong and duplicate values
- LINQ: Filter the list according to the math condition above elements in another list
- Get each item from a list and compare it with a different one
- Linq query help c#
- Convert loop to LINQ
- how to bind only 10 latest records to the model in C#
- What is the simplest way to achieve O(n) performance when creating the union of 3 IEnumerables?
- Find Items in List<T> based on id
- Finding objects which contains at least one element from subset using RavenDB and LINQ
- LINQ Subquery Where Clause could not be translated
- Linq Left Join Throws Error Saying 'There is already an open DataReader associated with this Connection which must be closed first.'
- Mocking and IQueryable<T>
- C# how to filter a list by different properties at runtime
- How can i do this with Extensions methods or Linq?
- LINQ expression for climbing object hierarchy?
- Linq woes - A CollectionType is required