简体   繁体   中英

cannot implicitly convert type System.Collections.Generic.List<char>

I am trying to create a list of customer names that is fetched from a Json call but I get an error:

cannot implicitly convert type System.Collections.Generic.List<char> to System.Collections.Generic.List<string>

I am using these 2 classes:

Customers:

namespace eko_app
{
    static class Customers
    {
        public static List<CustomerResponse> GetCustomers(string customerURL)
        {
            List<CustomerResponse> customers = new List<CustomerResponse>();

            try
            {
                var w = new WebClient();
                var jsonData = string.Empty;

                // make the select products call
                jsonData = w.DownloadString(customerURL);

                if (!string.IsNullOrEmpty(jsonData))
                {
                    // deserialize the json to c# .net
                    var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonData);

                    if (response != null)
                    {
                        customers = response.response;
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return customers;
        }

        public class BusinessAssociate
        {
            public string business_associate_id { get; set; }
            public string created_by { get; set; }
            public DateTime created { get; set; }
            public DateTime modified { get; set; }
            public bool? deleted { get; set; }
            public string business_id { get; set; }
            public string identity_id { get; set; }
            public string associate_type { get; set; }
            public string name { get; set; }
        }

        public class Identity
        {
            public string identity_id { get; set; }
            public string created_by { get; set; }
            public DateTime created { get; set; }
            public DateTime modified { get; set; }
            public bool? deleted { get; set; }
            public string name { get; set; }
            public object identity_type { get; set; }
        }

        public class ChartOfAccount
        {
            public string chart_of_accounts_id { get; set; }
            public DateTime created { get; set; }
            public DateTime modified { get; set; }
            public string created_by { get; set; }
            public string deleted { get; set; }
            public string account_id { get; set; }
            public string account_name { get; set; }
            public string business_id { get; set; }
            public string account_category { get; set; }
            public string accounts_groups_id { get; set; }
            public string cash_equivalent { get; set; }
            public string acc_category { get; set; }
            public decimal? balance { get; set; }
            public decimal? credit_balance { get; set; }
            public decimal? debit_balance { get; set; }
            public decimal? absolute_balance { get; set; }
            public string balance_type { get; set; }
            public decimal? raw_balance { get; set; }
            public string extended_name { get; set; }
            public string normal_balance_type { get; set; }
        }

        public class CustomerResponse
        {
            public BusinessAssociate BusinessAssociate { get; set; }
            public Identity Identity { get; set; }
            public ChartOfAccount ChartOfAccount { get; set; }
        }

        public class Messages
        {
            public string msgs { get; set; }
            public string errs { get; set; }
        }

        public class RootObject
        {
            public List<CustomerResponse> response { get; set; }
            public Messages messages { get; set; }
        }
    }
}

HomeForm:

 private void GetCustomerNameList()
        {
            // get customers
            customerURL = "https://eko-app.com/BusinessAssociate/list_associates/1/sessionId:" + sessionID + ".json";

            var customers = Customers.GetCustomers(customerURL);

            List<string> customerNames = new List<string>();

            foreach (var c in customers)
            {
                customerNames = c.BusinessAssociate.name.ToList(); <--------error thrown here
            }
        }

The error is thrown at customerNames = c.BusinessAssociate.name.ToList(); on the HomeForm.

What am I doing wrong in creating a list of customer names?

I think you wanted to add all Customer.BusinessAssociate names to list:

foreach (var c in customers)
{
    customerNames.Add(c.BusinessAssociate.name);
}

What you originally written converted each name string to char list .

You're assigning a list of chars (string) into a list of strings. Try something like this outside of the foreach loop:

customerNames = customers.Select(x => x.BusinessAssociate.name).ToList();

This also makes the initialization of cutomerNames redundant.

代替foreach使用:
customerNames = customers.Select(c => c.BusinessAssociate.name).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM