gpt4 book ai didi

c# - 在数据表上进行选择时出现异常

转载 作者:行者123 更新时间:2023-12-02 04:30:54 26 4
gpt4 key购买 nike

这是我的代码

var compainNames = (from row in DTgraph.AsEnumerable()
group row by row.Field<string>("Campaign") into grp
select new
{
CampaignName = grp.Key
}).ToList();

var dataForOneCampaint = DTgraph.Select("Campaign = " + compainNames[i].ToString()).ToList();

其中 DTgraph 是数据表。

我遇到了这个异常:

Cannot interpret token '{' at position 12.

你能帮忙吗?

我调试后可以看到 compainNames 有 3 个字符串

这一行的异常

 var dataForOneCampaint = DTgraph.Select("Campaign = " + compainNames[i].ToString()).ToList();

最佳答案

DataTable.Select方法使用与 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 (and each single quotation character in a string value has to be escaped by prepending it with another single quotation character).

我相信你可以使用;

var dataForOneCampaint = DTgraph
.Select("Campaign = '" + compainNames[i].ToString() + "'")
.ToList();

或者使用String.Format作为Felipe mentioned .

关于c# - 在数据表上进行选择时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23176355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com