i am developing silverlight application. right now, i have an issue to pass string variable to sql statement, together with double quotes. i tried using this way:
string itemList;
string commaString = @"""";
for (int i = 0; i < TestList.Count; i++)
{
itemList += commaString + TestList[i].Code + commaString + " || ";
}
itemList = "x.Code == " + itemList;
itemList = itemList.Removed(itemList.Length - 3);
but the variable passed is like this:
" x.Code == \\" 11001-111001 \\" || x.Code == \\" 11016-111001 \\" "
i want it to be this way:
x.Code == "11001-111001" || x.Code == "11016-111001"
i dont want the back slash. i want to pass this variable to sql select statement. is it possible to do this? can anybody help me? thank you...
***Updated:
my TestList currently have 2 values:
"11001-111001"
"11016-111001"
and then i want to combine these values in itemList to be:
x.Code == "11001-111001" || x.Code == "11016-111001"
since i want to use this in sql statement. when combined, now becomes
" x.Code == \\"11001-111001\\" || x.Code == \\"11016-111001\\" "
below is how i want to use the variable. i want to replace the codes with itemList :
private void GetAccountCodes()
{
var r = _svc.AccountCodes.Where(x => x.Code == "11001-111001" || x.Code == "11016-111001").Select(x => x);
_company.AccountCodes.LoadCompleted -= new EventHandler<LoadCompletedEventArgs>(AccountCodes_LoadCompleted);
_company.AccountCodes.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(AccountCodes_LoadCompleted);
_company.AccountCodes.Clear(true);
_company.AccountCodes.LoadAsync(r);
}
private void AccountCodes_LoadCompleted(object sender, LoadCompletedEventArgs e)
{
if (_company.AccountCodes!= null && _company.AccountCodes.Count() > 0)
{
//do something. but right now it returns no record
}
}
Edit:
You can use Contains for pass a list of items for a database linq query:
.Where(x=> itemList.Contains(x.Code)).ToList()
As for the `\\" part This is only the debugger showing the value like that.
Trying clicking the small magnifier icon or printing the value to the console, you'll get:
"11001-111001" || x.Code == "11016-111001" || "
See this image as a proof:
Those escape backslashes are added by the debugger only.
To add a single quote, you can use one of the below approaches:
string commaString = " \" "
Or:
string commaString = @" "" "
user3185569 is right. It's just the debugger that shows that value. Nevertheless it might not be the best idea to concatenate the strings that way (remember: strings are immutable). I would use a StringBuilder() and string.Format() as follows:
StringBuilder itemList = new StringBuilder();
for (int i = 0; i < TestList.Count; i++)
{
itemList.Append(string.Format("\"{0}\" || ", TestList[i].Code));
}
This also improves readability.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.