LINQ To SQL 包含区分大小写的搜索

LINQ To SQL Contains Case Sensitive Searching

当我使用下面的查询时,它得到了完美的结果。但它区分大小写。这是我的代码:

    IQueryable<article> results;
    if (rrbAuthor.IsChecked)
        results = (from a in _db.articles join k in _db.keywords on a.id equals k.aid where a.author.Contains(textBox1.Text) select a).Distinct();
    else if (rrbKeywords.IsChecked)
        results = (from a in _db.articles join k in _db.keywords on a.id equals k.aid where k.word.Contains(textBox1.Text) select a).Distinct();
    else
        results = (from a in _db.articles join k in _db.keywords on a.id equals k.aid where a.title.Contains(textBox1.Text) select a).Distinct();

    ListArticles(results, 1);

如何在不敏感的情况下获得结果?

您可以使用 ToLower() 方法将字符串转换为小写,然后进行比较
您还可以使用 null propagation 来避免 NullReference 异常,以防某些 stirngs 为空。

尝试关注

  IQueryable<article> results;
  if (rrbAuthor.IsChecked)
    results = (from a in _db.articles join k in _db.keywords on a.id equals k.aid
               where a.author?.ToLower().Contains(textBox1.Text?.ToLower()) == true
               select a).Distinct();
  else if (rrbKeywords.IsChecked)
    results = (from a in _db.articles join k in _db.keywords on a.id equals k.aid
               where k.word?.ToLower().Contains(textBox1.Text?.ToLower()) == true
               select a).Distinct();
  else
    results = (from a in _db.articles join k in _db.keywords on a.id equals k.aid
               where a.title?.ToLower().Contains(textBox1.Text?.ToLower()) == true
               select a).Distinct();
  ListArticles(results, 1);

你可以在两侧使用.ToUpper() 或.ToLower():

    results = (from a in _db.articles join k in _db.keywords on a.id equals k.aid 
where a.author.ToUpper().Contains(textBox1.Text.ToUpper()) select a).Distinct();

或者

    results = (from a in _db.articles join k in _db.keywords on a.id equals k.aid 
where a.author.ToLower().Contains(textBox1.Text.ToLower()) select a).Distinct();