将string []转换为Array

-6 c# arrays type-conversion

我目前在数据类型方面有点迷失.

在我正在构建的应用程序中,我有一个必须运行查询的功能,具体取决于Array客户ID.

数据必须在某种条件下被覆盖,数组由ID字符串组成.

问题是Split()函数返回a string[],而我的数据是a Array.我可以分配新值data,如下所示,但我不能再访问数据了.

这是因为对于a Array,我必须使用data.GetValue(i),而a string[]只能访问data[i].

剥离代码:

internal void GetData(Array data) {

    string ids == "123,456,789";

    if(condition){
        data = (Array) ids.Split(',').ToArray(); // Trying to convert it to a Array, can't figure it out...
    }

    // Accessing the data at index `i`
    data.GetValue(i); // Default case, works when the condition is false
    data[i];          //     This only works when the condition is true
}
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是,有没有办法让我将我的分裂字符串保存为Array而不是string[]


完整代码:

internal M.RequestCustomQueryResultsList GetData(Array data) {
    M.RequestCustomQueryResultsList result = new M.RequestCustomQueryResultsList();
    result.CustomQueryResults = new List<CustomQueryResult>();

    SqlConnection conn = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand();

    conn.Open();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = conn;
    cmd.CommandText = queriesQuery; // StacOverflow: A query to obtain some queries

    SqlDataReader reader = cmd.ExecuteReader();
    while(reader.Read()) {
        // For each query
        SqlConnection conn1 = new SqlConnection(connectionString);
        conn1.Open();

        /* StackOverflow: The real relevant stuff starts here. */

        String query = reader["Query"].ToString(); // Get the current query

        Match match = Regex.Match(query, @"{((?:\w,?)*)}");

        if(match.Success) { // If the query contains "{0}" or "{123,456,789,etc}"
            string m = match.Groups[1].Value; // get the value in the "{}"
            if(!m.Equals("0")){ // If that value isn't "123,456,etc"
                data = m.Split(','); // Split up the id's into an array.
            }
            for(int i = 0; i < data.Length; i++) { // Loop through the data (Works if m.Equals("0"))
                // Execute the query for every company.
                String q = string.Format(query, data.GetValue(i).ToString()); // Fails
                CustomQueryResult c = new CustomQueryResult();

                c.CustNo = int.Parse(data.GetValue(i).ToString());  // Fails

                // Here I set some more values to c, add c  to `result`, and close all loops / etc.
Run Code Online (Sandbox Code Playgroud)

GetData是作为GetCustomQueriesResults服务请求调用的一部分.

public T.RequestCustomQueryResultsList GetCustomQueriesResults(Array data) {
    var CustomQueriesResultsList = new S.CustomQueriesResultsList(data);
    return CustomQueriesResultsList.GetData(data);
}
Run Code Online (Sandbox Code Playgroud)

我从JavaScript调用服务,如下所示:

repo.GetCustomQueriesResults([123,456,789]);
Run Code Online (Sandbox Code Playgroud)

repo构建一个使用JSON.stringify(data)as data参数的ajax请求.

Jon*_*eet 5

A string[]已经是Array(因为每个具体的数组类型派生自Array) - 并且Split已经给出了一个string[],所以你不需要调用ToArray结果.

所以这:

data = (Array) ids.Split(',').ToArray();
Run Code Online (Sandbox Code Playgroud)

可以只是:

data = ids.Split(',');
Run Code Online (Sandbox Code Playgroud)

结果一个数组,因此没有其他工作要做.

请注意,如果出现问题,更改值data不会更改调用者的数组.您应该阅读我关于参数传递的文章,以了解有关这方面的更多信息.

编辑:既然我们可以看到你的代码,我相信这个问题与数组没什么关系.就是这样:

String q = string.Format(query, data.GetValue(i).ToString()); // Fails
Run Code Online (Sandbox Code Playgroud)

强烈怀疑您的查询包含的{1}内容正在尝试使用不存在的参数进行格式化.

验证这一点应该很容易 - 只需将部分分开:

String value = data.GetValue(i).ToString();
String q = string.Format(query, value);
Run Code Online (Sandbox Code Playgroud)