列表来处理双打数组 [英] list to handle array of doubles
问题描述
有没有办法处理双打列表,我可以使用下面的代码处理整数,但不确定如何处理双打数组.
Is there a way to handle a list of doubles I can handle ints with the code below but not sure how to handle array of doubles.
{
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的双精度阵列错误吗?
This is all wrong tho I need instead of ArrayOfInt to have Array of 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);
}
}
}
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();
}
}
}
推荐答案
我不清楚您的问题是什么,但是:
I'm not clear what your question is, but:
如果 arrayOfInt.AddRange(listOfDouble)
未编译,则可以使用Linq并执行
If arrayOfInt.AddRange( listOfDouble )
is not compiling you could use Linq and do
arrayOfInt.AddRange( listOfDouble.Select( d => (int)d ).ToList() );
client.CalculateSum方法需要类型为 List< double>
The method client.CalculateSum expects a parameter of type List<double>
1)如果可能,我将其更改为更灵活的
1) I would change that if possible to be public string CalculateSum(IEnumerable<double> items)
which is more flexible
2)如上,您可以使用 list.Select(n =>(double)n).ToList()
转换任何可转换类型的列表,假设(double)n
是有效的演员表.
2) As above you can convert a list of any convertible type using list.Select(n => (double)n).ToList()
assuming that (double)n
is a valid cast.
这篇关于列表来处理双打数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!