简体   繁体   中英

Convert in ObservableCollection (C# uwp)

I am writing an app for UWP.

I tried to use data binding according to this answer link .

Here are my classes:

 public class Billing
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string company { get; set; }
        public string address_1 { get; set; }
        public string address_2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string postcode { get; set; }
        public string country { get; set; }
        public string email { get; set; }
        public string phone { get; set; }          
    }

    public class Shipping
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string company { get; set; }
        public string address_1 { get; set; }
        public string address_2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string postcode { get; set; }
        public string country { get; set; }
    }

    public class RootObject
    {
        public int id { get; set; }
        public int parent_id { get; set; }
        public string status { get; set; }

        public string order_key { get; set; }
        public string currency { get; set; }
        public string version { get; set; }
        public bool prices_include_tax { get; set; }
        public string date_created { get; set; }
        public string date_modified { get; set; }
        public int customer_id { get; set; }
        public double discount_total { get; set; }
        public double discount_tax { get; set; }
        public double shipping_total { get; set; }
        public double shipping_tax { get; set; }
        public double cart_tax { get; set; }
        public double total { get; set; }
        public double total_tax { get; set; }
        public Billing billing { get; set; }
        public Shipping shipping { get; set; }
        public string payment_method { get; set; }
        public string payment_method_title { get; set; }
        public string transaction_id { get; set; }
        public string customer_ip_address { get; set; }
        public string customer_user_agent { get; set; }
        public string created_via { get; set; }
        public string customer_note { get; set; }
        public string date_completed { get; set; }
        public string date_paid { get; set; }
        public string cart_hash { get; set; }
        public List<object> line_items { get; set; }
        public List<object> tax_lines { get; set; }
        public List<object> shipping_lines { get; set; }
        public List<object> fee_lines { get; set; }
        public List<object> coupon_lines { get; set; }
    }

    public ObservableCollection<RootObject> Orders { get; set; }

Here is the code:

List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(products);
foreach (RootObject root in rootObjectData)
{
    string date = root.date_created;
    string name = root.billing.first_name + root.billing.last_name ;
    Orders = new ObservableCollection<RootObject> { new RootObject { date_created = date,billing = name } };
}

With billing = name I have this error: Cannot implicitly convert type 'string' to 'Milano.InWork.Billing'

How can I fix this error? Maybe this is simple, but I don't find a solution. Thanks for the help!!

With billing = name I have this error: Cannot implicitly convert type 'string' to 'Milano.InWork.Billing'

Of course this error will occur, your billing in the class RootObject has the data type Billing , which is another class you defined.

Just from your code I think you only want to show the first_name and last_name as string in your root.billing , then you can change the code public Billing billing { get; set; } public Billing billing { get; set; } public Billing billing { get; set; } in your RootObject class to public string billing { get; set; } public string billing { get; set; } public string billing { get; set; } .

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