列表来处理双打数组
本文关键字:数组 处理 列表 | 更新日期: 2023-09-27 18:26:40
有没有一种方法可以处理双打列表我可以用下面的代码处理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,而是双数组?
客户端:
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);
}
}
}
Web方法:
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();
}
}
}
我不清楚你的问题是什么,但是:
如果arrayOfInt.AddRange( listOfDouble )
没有编译,您可以使用Linq并进行
arrayOfInt.AddRange( listOfDouble.Select( d => (int)d ).ToList() );
方法客户端。CalculateSum需要List<double>
类型的参数
1) 如果可能的话,我会把它改为更灵活的 public string CalculateSum(IEnumerable<double> items)
2) 如上所述,假设(double)n
是有效的强制转换,则可以使用list.Select(n => (double)n).ToList()
转换任何可转换类型的列表。
如果我正确理解您的问题,您需要将ConvertAll调用为一种十进制类型,然后才能添加listOfDecimal。
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);