[英]Is it possible to convert a MVC C# List<int> to an javascript array?
[英]Convert list to int array in C#
我已經通過StackOverflow和Google搜索了轉換,但遺憾的是我無法獲得解決方案。 一切都與我想要的相反。 即,將int[]
轉換為List
。
問題
我在[WebMethod]
有這個代碼。
[WebMethod]
public int MyMethodWS(int N, List<int> M)
{
}
現在我有一個控制台應用程序 ,它使用以下URL引用此服務:
http://localhost:61090/MyMethod.asmx?WSDL
我的控制台應用程序中有此代碼:
int N;
List<int> M = new List<int>();
// Some crazy user input coding.
// Ultimately you will have non-empty M list and N int.
MyMethod.MyMethodWS DA = new MyMethod.MyMethodWS();
Sum = DA.MyMethodWS(N, M);
當我運行此代碼時,我收到此錯誤:
#1: '
MyMethod.MyMethod.MyMethodWS(int, int[])
'的最佳重載方法匹配有一些無效的參數。#2:參數2:無法從'
System.Collections.Generic.List<int>
'轉換為'int[]
'
問題
List
。 但為什么它試圖轉換為int[]
? 它試圖將其轉換為int[]
,因為MyMethodWS
將int[]
作為參數,而不是您嘗試傳遞它的List<int>
。 要將List<int>
轉換為int[]
,請調用List<T>.ToArray();
所以改變行Sum = DA.MyMethodWS(N, M);
to Sum = DA.MyMethodWS(N, M.ToArray());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.