有没有办法处理我可以使用下面的代码处理int的双打列表,但不知道如何处理双打数组。
{
CalculateSumOfList.ServiceReference1.Service1SoapClient client = new CalculateSumOfList.ServiceReference1.Service1SoapClient();
CalculateSumOfList.ServiceReference1.ArrayOfInt arrayOfInt = new CalculateSumOfList.ServiceReference1.ArrayOfInt();
arrayOfInt.AddRange(listDouble); // error here!
string result = client.CalculateSum(arrayOfInt);
label1.Text = Convert.ToString(result);
}
这是错误的我需要而不是ArrayOfInt才能拥有Double的数组?
客户端:
namespace CalculateSumOfList
{
public partial class Form1 : Form
{
List<Double> listDouble = new List<Double>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
listDouble.Add(Convert.ToDouble(textBox1.Text));
textBox1.Clear();
listBox1.Items.Clear();
for (int i = 0; i < listDouble.Count; i++)
{
listBox1.Items.Add(listDouble[i]);
}
textBox1.Clear();
listBox1.Items.Clear();
for (int i = 0; i < listDouble.Count; i++)
{
listBox1.Items.Add(listDouble[i]);
}
}
private void button2_Click(object sender, EventArgs e)
{
CalculateSumOfList.ServiceReference1.Service1SoapClient client = new CalculateSumOfList.ServiceReference1.Service1SoapClient();
CalculateSumOfList.ServiceReference1.ArrayOfInt arrayOfInt = new CalculateSumOfList.ServiceReference1.ArrayOfInt();
arrayOfInt.AddRange(listDouble); // error here!
string result = client.CalculateSum(arrayOfInt);
label1.Text = Convert.ToString(result);
}
}
}
网络方法:
namespace CalculateWebServiceSum
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string CalculateSum(List<double> listDouble)
{
return listDouble.Sum().ToString();
}
}
}
答案 0 :(得分:1)
如果我理解你的问题,你需要在添加listOfDecimal之前调用ConvertAll到十进制类型。
var convertedDecimalList = arrayOfInt.ConvertAll<decimal>
(element => (decimal)element);
convertedDecimalList.AddRange(listDecimal);
更新:
CalculateSumOfList.ServiceReference1.Service1SoapClient client =
new CalculateSumOfList.ServiceReference1.Service1SoapClient();
CalculateSumOfList.ServiceReference1.ArrayOfInt arrayOfInt =
new CalculateSumOfList.ServiceReference1.ArrayOfInt();
var listOfIntAsDouble = arrayOfInt.ConvertAll(x=>Convert.ToDouble(x));
listOfIntAsDouble .AddRange(listDouble);
string result = client.CalculateSum(listOfIntAsDouble);
label1.Text = Convert.ToString(result);
答案 1 :(得分:1)
我不清楚你的问题是什么,但是:
如果arrayOfInt.AddRange( listOfDouble )
未编译,您可以使用Linq并执行
arrayOfInt.AddRange( listOfDouble.Select( d => (int)d ).ToList() );
方法client.CalculateSum需要类型为List<double>
1)如果可能,我会更改public string CalculateSum(IEnumerable<double> items)
更灵活
2)如上所述,您可以使用list.Select(n => (double)n).ToList()
转换任何可转换类型的列表,假设(double)n
是有效演员。