Linq-to-SQL反向包含 [英] Linq-to-SQL reverse contains
本文介绍了Linq-to-SQL反向包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Linq-to-SQL查询中反转包含内容,以便我可以检查Title
或Description
是否包含列表中的任何单词,例如:
How would I reverse contains in a Linq-to-SQL query so that I can check if Title
or Description
contain any word from my list, something like:
var query = context.Shapes.Where(x => x.Title.Contains(words));
这是我现在拥有的东西,但这与我所需要的相反.
Here is what I have now but that is opposite from what I need.
List<string> words = Search.GetTags(q);
//words = round,circle,square
using(ShapesDataContext context = new ShapesDataContext())
{
var query = context.Shapes.Where(x => words.Contains(x.Title) ||
words.Contains(x.Description));
}
// Item 1: Title = Elipse , Decsription = This is not round circle
//This should be a match! but words doesn't contain
//"This is not round circle", only round and circle so no match
更新
现在我有
var query = context.Shapes.Where(x => words.Any(w => x.Title.Contains(w) || x.Description.Contains(w)))
int s = query.Count();
但是现在我在int s = query.Count();
上遇到了异常,消息为不能在包含包含运算符的查询运算符的LINQ to SQL实现中使用本地序列".有人知道如何解决吗?
but now I get exception on int s = query.Count();
with message "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator." Does anyone know how to solve it?
推荐答案
不是最有效的方法,但我设法做到了:
Not the most efficient but I managed:
List<string> words = Search.GetTags(q);
using(ShapesDataContext context = new ShapesDataContext())
{
IQueryable<Shape> query = Enumerable.Empty<Shape>().AsQueryable();
foreach (var word in words)
{
query = query.Union(context.Shapes.Where(x => x.Title.Contains(word) || x.Description.Contains(word)));
}
这篇关于Linq-to-SQL反向包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文