Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 12-Sep-18 4:38am
Comments
Alek Massey 12-Sep-18 10:16am    
Likely suspects, the ' is not a single quote in your db or the - is not a dash in your db.

Debug your app and see what vList looks like with a broader 'contains' expression statement.
Something like,
var expression = "ClaimInfo"var vList= (from vocab in db.Vocabularies           where vocab.Expression.ToUpper().Contains(expression.ToUpper())           select vocab).ToList();


Compare that result to using the following:
var expression = "'ClaimInfo'"

and
var expression = "local-name"
RohitVaidya 21-Sep-18 5:24am    
Thanks Alek for comment. the ' is single quote only and same is true about - as well. Before posting question I have debug as well and tried different solution as I mentioned in the post

1 solution

Single quotes should work with .Contains() extension method, not unless if the text contains double quotes.

I've just verified with this example:

C#
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']
	}
}


You need to verify the actual value of vocab.Expression that you compare to your expression value that you passed into your Contains method.
 
Share this answer
 
v2
Comments
RohitVaidya 21-Sep-18 5:21am    
Hi Vincent, thanks for help. It is working in case of the sample you provided as it is a simple list and not the data coming from database entity.
I have checked vocab.Expression with expression value I am passing, both are similar.
In SQL server programming, for ' character we need to use escape sequence in where clause, but in case of c# linq statement with db entity it is giving problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900