[英]Datatable option results in "Missing operand after 's' operator" error
I have a datatable and I want to filter this table with a company name.我有一个数据表,我想用公司名称过滤这个表。 But it gave this error:
但它给出了这个错误:
Syntax error: Missing operand after 's' operator.
语法错误:'s' 运算符后缺少操作数。
My code is like this:我的代码是这样的:
DataRow[] rowList = resultDt.Select(string.Format(" [{0}] = '{1}'", resultDt.Columns["Company"], "Dyn's"));
What is be the problem?是什么问题?
The problem is " Dyn's
".问题是“
Dyn's
”。 Can you use \\'
instead of '
?你能用
\\'
代替'
吗? That apostrophe is why the problem is occurring.那个撇号是问题发生的原因。
It's the apostrophe in the name, you need to escape it:这是名称中的撇号,您需要对其进行转义:
DataRow[] rowList = resultDt.Select(string.Format(" [{0}] = '{1}'", resultDt.Columns["Company"], "Dyn\'s"))
The issue is it's being substituted for {1}
and so the resulting string looks like 'Dyn's'
, escaping it gets you to 'Dyn\\'s'
which should be okay.问题是它被替换为
{1}
,所以结果字符串看起来像'Dyn's'
,转义它会让你得到'Dyn\\'s'
这应该没问题。
Dyn's
您需要转义撇号:
Dyn\'s
尝试转义 ':
"Dyn\'s"
Are you able to use LINQ to DataTable instead?你能改用 LINQ to DataTable 吗? I've always thought that
DataTable.Select()
is an error-prone way of doing things, precisely for this sort of reason.我一直认为
DataTable.Select()
是一种容易出错的做事方式,正是出于这种原因。
Querying using LINQ to Objects (effectively) via the DataTableExtensions.AsEnumerable()
method (or more simply using strongly typed data sets, IIRC) is nicer IMO.通过
DataTableExtensions.AsEnumerable()
方法(或更简单地使用强类型数据集 IIRC)使用 LINQ to Objects(有效地)进行查询是更好的 IMO。 Admittedly you won't get the benefit of any in-memory indexing performed by the DataTable
, but I suspect in the majority of cases that will be irrelevant.诚然,您不会从
DataTable
执行的任何内存中索引中受益,但我怀疑在大多数情况下这无关紧要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.