list to handle array of doubles

∥☆過路亽.° 提交于 2019-12-12 03:09:21

问题


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);

    }

This is all wrong tho I need instead of ArrayOfInt to have Array of double?

Client Side:

    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 Method:

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();



        }
    }
}

回答1:


I'm not clear what your question is, but:

If arrayOfInt.AddRange( listOfDouble ) is not compiling you could use Linq and do

arrayOfInt.AddRange( listOfDouble.Select( d => (int)d ).ToList() );

The method client.CalculateSum expects a parameter of type List<double>

1) I would change that if possible to be public string CalculateSum(IEnumerable<double> items) which is more flexible

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.




回答2:


If I understand your question properly, you need to call ConvertAll to a type of decimal before you can add the listOfDecimal.

var convertedDecimalList = arrayOfInt.ConvertAll<decimal>
    (element => (decimal)element);
convertedDecimalList.AddRange(listDecimal);

UPDATED:

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);


来源:https://stackoverflow.com/questions/9570791/list-to-handle-array-of-doubles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!