c# DataTable select 不适用于特殊字符 #

发布于 2024-09-25 04:52:44 字数 289 浏览 1 评论 0原文

我有一个数据表选择,例如:

productData.Select("Name = 'AAA BBB # CCC'");

我知道该条目在那里,只是由于 # 字符而不起作用。我已经尝试使用 [] 转义:

productData.Select("Name = 'AAA BBB [#] CCC'");

但它仍然不起作用。我知道对于单引号,我将它们加倍,这样 ' 就变成了 ''。但是我还需要关心哪些其他角色以及如何让这个案例发挥作用。

I have a datatable select like:

productData.Select("Name = 'AAA BBB # CCC'");

I know the entry is there, it just doesn't work because of the # character. I have tried escaping with [] like:

productData.Select("Name = 'AAA BBB [#] CCC'");

but it still doesn't work. I know for single quotes I double them so ' becomes ''. But what other characters do I need to be concerned with and how to get this case to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-10-02 04:52:44

您绝对必须像这样使用DataTables吗?正是由于这个原因,我一直对 DataTable 中基于文本的查询感到非常紧张。

如果可能的话,我建议您开始使用 LINQ。您已经可以使用 DataTable 来做到这一点,例如

var query = products.AsEnumerable()
                    .Where(row => row.Field<string>("Name") == "AAA BBB # CCC");

这样您就不必担心转义等。如果您使用强类型数据集,它会变得更加简单,因为您可以直接引用属性使用字符串名称。

Do you absolutely have to use DataTables like this? I've always been incredibly nervous of the text-based querying in DataTable for precisely this reason.

If at all possible, I suggest you start using LINQ. You can do that with DataTable already, e.g.

var query = products.AsEnumerable()
                    .Where(row => row.Field<string>("Name") == "AAA BBB # CCC");

That way you don't need to worry about escaping etc. If you use a strongly-typed dataset it becomes even simpler, as you can refer to properties directly instead of using string names.

眼趣 2024-10-02 04:52:44

你尝试过这样的事情吗?

productData.Select(@"Name = 'AAA BBB # CCC'");

Have you tried something like this?

productData.Select(@"Name = 'AAA BBB # CCC'");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文