问题处理单引号包含LINQ C的函数# [英] Problem handling single quote in contains function of LINQ C#
问题描述
您好我在使用LINQ搜索记录时遇到问题。问题是当搜索字符串包含例如单引号时 // * [local-name()='ClaimInfo'] ,即使我在表中记录了相同的值,我也得到0结果。示例代码块如下所示
Hi I am facing issue while searching for record using LINQ. Issue is that when search string contains single quote for e.g. //*[local-name()='ClaimInfo'], then I get 0 result for it even after I have record in the table with the same value. Sample code block is given below
var expression = "//*[local-name()='ClaimInfo']"
var vList= (from vocab in db.Vocabularies
where vocab.Expression.ToUpper().Contains(expression.ToUpper())
select vocab).ToList();
我的尝试:
我尝试过替换功能,如下面给出的代码所示,但对我没有结果。 :-(
What I have tried:
I have tried Replace function as shown in code given below, but no result for me. :-(
var vList= (from vocab in db.Vocabularies
where vocab.Expression.Replace("'","''").ToUpper().Contains(expression.Replace("'", "''").ToUpper())
select vocab).ToList();
我尝试过替换(',@/)和替换(',),但没有运气。
I have tried Replace("'", @"/'") and Replace("'", "") as well but no luck.
推荐答案
单引号应该与.Contains()
扩展方法一起使用,除非文本包含双引号。
我刚用这个例子验证过:
Single quotes should work with.Contains()
extension method, not unless if the text contains double quotes.
I've just verified with this example:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
string expression = "//*[local-name()='ClaimInfo']";
List<string> listExp = new List<string>();
listExp.Add("a");
listExp.Add("b");
listExp.Add("c''");
listExp.Add("//*[local-name()='ClaimInfo']");
var result = listExp.Where(o => o.Contains(expression));
if(result.Any())
Console.WriteLine(result.First()); //prints //*[local-name()='ClaimInfo']
}
}
您需要验证 vocab.Expression $ c $的实际值c>您与传递给Contains方法的
表达式
值进行比较。
You need to verify the actual value of vocab.Expression
that you compare to your expression
value that you passed into your Contains method.
这篇关于问题处理单引号包含LINQ C的函数#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!