c# - DataTable 表达式无法解释标记 '!'

标签 c# datatable syntax-error expression token

我有以下代码:

//myDataTable has the following collumns: UserName, Birthday and email.
string name = "eric!";
string expression = "UserName = " + name;
DataRow[] result = myDataTable.Select(expression);

我想选择名称为“eric!”的所有行。
这 ”!”给我以下错误:

Cannot interpret token "!".

如何选择所有带有此类标记的行?
(我真的需要表达式中的“!”,因为我是从 .sql 文件中提取用户名的)

最佳答案

您应该在 '' 之间使用您的 name。喜欢;

string name = "'eric!'";

没有单引号,你的 DataTable.Select方法认为 ! 是一个 operator 并且在 DataColumn.Expression property 中是不允许的作为有效的运算符。

来自文档

User-Defined Values

User-defined values may be used within expressions to be compared with column values. String values should be enclosed within single quotation marks.

关于c# - DataTable 表达式无法解释标记 '!',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21645666/

相关文章:

javascript - 无限滚动分页 ag-Grid 初始页面大小问题

c# - 仅使用新记录进行更新时,DataTable.Load 方法不起作用

javascript - "SyntaxError: Unexpected token <"与 Ionic Angular JS

c - 时间表测验(错误)

c# - 使用 mvc 未登录就打开错误主页?

c# - 为什么 CheckBox Html Helper 会呈现隐藏的输入字段?

c# - 在 Visual Studio 中打开解决方案时如何自动运行 dotnet 命令

javascript - 避免 CSS 的自定义标题高度

javascript - Python Selenium 网络驱动程序 : trying to call a javascript function

c# - Foreach 循环和 lambda 表达式