1

我将如何在 Linq-to-SQL 查询中反转包含,以便我可以检查是否TitleDescription包含列表中的任何单词,例如:

var query = context.Shapes.Where(x => x.Title.Contains(words));

这是我现在所拥有的,但这与我所需要的相反。

 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 实现中使用,但包含运算符除外。” 有谁知道如何解决它?

4

4 回答 4

6

你要

x => words.Any(w => x.Title.Contains(w) || x.Description.Contains(w))
于 2012-06-08T14:56:26.097 回答
1

不是最有效的,但我做到了:

 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)));
   }
于 2012-06-08T15:43:48.653 回答
0

你在寻找类似 NOT-IN 集合查询的东西吗?

那么这篇博文可能会有所帮助

http://introducinglinq.com/blogs/marcorusso/archive/2008/01/14/the-not-in-clause-in-linq-to-sql.aspx

高温高压

于 2012-06-08T14:58:12.783 回答
0

我的解决方案是使用子查询(子选择)

  dim searchTerms as new list of(string) 'search terms here

  dim Result = (From x In DB.items Where 
                    (
                      searchTerms.Count = 0 Or
                      (From z In searchTerms Where x.SearchableText.Contains(z) Select z).Count > 0
                    )
                    Select x).ToList()
于 2014-11-01T19:49:46.480 回答