【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List<string[]>' to 'string[]'无法将类型“System.Collections.Generic.List<string[]>”隐式转换为“string[]”
【发布时间】:2013-04-21 21:51:17
【问题描述】:

下面是我的代码,它给出了一个错误: 无法将类型“System.Collections.Generic.List”隐式转换为“string[]”

我尝试了几次来解决这个错误。但我没有这样做。 如果有任何机构可以提出解决方案..谢谢:)

public GetContentResponse GetContent(abcServiceClient client, QueryPresentationElementIDsResponse querypresentationelementresponse)
        {
            GetContentRequest GetContentRequest = new GetContentRequest();
            GetContentResponse contentresponse = new GetContentResponse();
            querypresentationelementresponse = presentationElementId(client);
            List<string[]> chunks = new List<string[]>();
            for (int i = 0; i < querypresentationelementresponse.IDs.Length; i += 25)
            {
                chunks.Add(querypresentationelementresponse.IDs.Skip(i).Take(25).ToArray());
                contentresponse = client.GetContent(new GetContentRequest()
                {
                    IDs = chunks // here i get this error
                });
            }

            return contentresponse;
        }

【问题讨论】:

    标签: c# asp.net web-services casting


    【解决方案1】:

    您正在尝试将 List 分配给字符串数组。将列表转换为数组。 由于您没有确切指定错误的位置,我想是在您分配 IDs 变量时。

    下面的代码可以解决它:

    public GetContentResponse GetContent(abcServiceClient client, QueryPresentationElementIDsResponse querypresentationelementresponse)
            {
                GetContentRequest GetContentRequest = new GetContentRequest();
                GetContentResponse contentresponse = new GetContentResponse();
                querypresentationelementresponse = presentationElementId(client);
                List<string> chunks = new List<string>();
                for (int i = 0; i < querypresentationelementresponse.IDs.Length; i += 25)
                {
                    chunks.AddRange(querypresentationelementresponse.IDs.Skip(i).Take(25));
                    contentresponse = client.GetContent(new GetContentRequest()
                    {
                        IDs = chunks.ToArray()
                    });
                }
    
                return contentresponse;
            }
    

    【讨论】:

    • 我也做了同样的事情..但这并没有帮助我。如果您建议这样做,我仍然可以再试一次。 :) 非常感谢。
    • 按照你说的做后显示的错误。无法将类型 'string[][]' 隐式转换为 'string[]'
    • 我在写入 IDs = 块时收到此错误。在问题中也提到了具体错误出现的注释行。
    • 啊,现在我明白了,你实际上有一个字符串数组列表。我改变了答案。您只需要制作一个字符串列表并为其添加范围
    • Kenneth,您的解决方案虽然有效.. 构建良好,但也需要少量修改.. :) 无论如何谢谢 :D
    【解决方案2】:

    我不确定是什么类型 ID 或哪一行给了你这个错误,但如果我不得不猜测,我认为它是 IDs = chunks

    听起来您正在尝试将列表转换为字符串数组。您需要使用 toArray() 的方法进行转换。

    编辑:好的,你有一个字符串数组。您需要通过以下方式获取正确的:

    IDs = Chunks.ToArray()[index]
    

    其中 index 是正确的数组。对您正在使用的库不是很熟悉,所以很遗憾我无法详细说明。但是,为了大胆猜测,请尝试使用 i 代替 index

    【讨论】:

    • Blast,Kenneth,你打败了我提交按钮:P。
    • 还有机会,克里斯。我敢打赌IDstring[],而chunks.ToArray() 只会返回string[][]
    • 但是这个解决方案并没有帮助 :p 再试一次可能会让你赢 :D
    • @ChrisChambers,我已经尝试过了,但这甚至不能很好地适用于场景。 :) 非常感谢您的快速回复.. :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    相关资源
    最近更新 更多