I have a query, which throws an exception in Linq to开发者_Python百科 Sql.
Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.
I have seen plenty of related questions so I (roughly) understand the reason WHY it doesn't work (the value term
in the lambda expression cannot be converted into a string value for the SQL query), but I cannot see HOW to should restructure this.
var searchTerms = userQuery.Split(' ').ToList().ConvertAll(term => term.ToLower());
var qry = (from tree in someQueryable
join widget in myDb.Widgets
on tree.ParentId equals widget.Id
where
searchTerms.All(term => tree.Title.ToLower().Contains(term) //Can't use Contains on term, as term isn't a local variable
|| searchTerms.All(term => widget.Title.ToLower().Contains(term) //And again here
|| (tree.Description != null && searchTerms.All(term =>
tree.Description.ToLower().Contains(term))) //And here
orderby tree.SomeDate descending
select tree);
How can I get all trees
whose Title, Description or ParentWidget.Title contain ALL of the search terms?
I have tried iterating over each term, however this gives me the issue of matching a single term, not all terms.
Update I resolved the issue... by changing my requirements :) I am still very interested to know how my initial requirements could be achieved.
from term in SearchTerms
from tree in someQueryable
join widget in myDb.Widgets
where tree.Title.ToLower().Contains(term) ||
widget.Title.TowLower().Contains(term) ||
(tree.Description != null && tree.Description.ToLower.Contains(term))
orderby tree.SomeDate descending
select tree
精彩评论