简体   繁体   中英

C# Linq Syntax Join

I have a List<Customer> that has a property of another child List<CustomerOrder> . I then have a list of customer order and errors List<OrderError> . How can I best update the child List with the error message. This is what it looks like:

Customer Class:
CustomerId
CustomerName
CustomerLocation
List<CustomerOrder> Orders

CustomerOrder Class:
OrderId
OrderStatus
ErrorMessage

OrderError Class:
OrderId
ErrorMessage

What I need to do is update the CustomerOrder class OrderStatus where if I don't have an error it will be set to "P" if I do have an error in List I want to update the OrderStatus to "F" and then the ErrorMessage. I am wondering if instead of doing a 'new' List with join and I believe a DefaultEmpty if there is a better way to do this.

Code:

var customers = new List<Customer>();
Customer cust1 = new Customer();
cust1.CustomerId = 1;
cust1.CustomerName = "ABC Corp";
cust1.CustomerLocation = "Raliegh";

var customerOrders = new List<CustomerOrder>();
CustomerOrder custOrd1 = new CustomerOrder();
custOrd1.OrderId = "152654";
customerOrders.Add(custOrd1);

CustomerOrder custOrd2 = new CustomerOrder();
custOrd2.OrderId = "155000";
customerOrders.Add(custOrd2);

cust1.Orders = customerOrders;
customers.Add(cust1);

Customer cust2 = new Customer();
cust2.CustomerId = 2;
cust2.CustomerName = "DEF Corp";
cust2.CustomerLocation = "Tilo";

customerOrders = new List<CustomerOrder>();
custOrd1 = new CustomerOrder();
custOrd1.OrderId = "200012";
customerOrders.Add(custOrd1);

cust2.Orders = customerOrders;
customers.Add(cust2);

Then the OrderErrors List:

var orderErrors = new List<OrderError>();
OrderError err = new OrderError();
err.OrderId = "155000";
err.ErrorMessage = "Order Failed Validation";

orderErrors.Add(err);

I need to update after the orderErrors is populated like above the OrderId in the customers list.

I would use a dictionary :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<CustomerOrder> orders = new List<CustomerOrder>();
            List<OrderError> orderErrors = new List<OrderError>();

            Dictionary<string, OrderError> dictOrderErrors = orderErrors
                .GroupBy(x => x.OrderId, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            foreach (CustomerOrder order in orders)
            {
                order.ErrorMessage = dictOrderErrors[order.OrderId].ErrorMessage;
            }

        }
    }
    public class CustomerOrder
    {
        public string OrderId { get; set; }
        public string OrderStatus { get; set; }
        public string ErrorMessage { get; set; }
    }
    public class OrderError
    {
        public string OrderId { get; set; }
        public string ErrorMessage { 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