簡體   English   中英

Linq-to-SQL反向包含

[英]Linq-to-SQL reverse contains

我將如何在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實現中使用本地序列”。 有人知道如何解決嗎?

你要

x => words.Any(w => x.Title.Contains(w) || x.Description.Contains(w))

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

 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)));
   }

您是否正在尋找類似NOT-IN集合查詢的內容?

然后,此博客文章可能會有所幫助

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

高溫超導

我的解決方案是使用子查詢(子選擇)

  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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM