[英]C# Linq Syntax Join
I have a List<Customer>
that has a property of another child List<CustomerOrder>
.我有一个
List<Customer>
,它具有另一个子List<CustomerOrder>
的属性。 I then have a list of customer order and errors List<OrderError>
.然后我有一个客户订单和错误
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.我需要做的是更新 CustomerOrder 类 OrderStatus,如果我没有错误,它将被设置为“P”,如果我在 List 中有错误我想将 OrderStatus 更新为“F”,然后是 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.
我想知道是否不是用 join 做一个“新”列表,我相信 DefaultEmpty 如果有更好的方法来做到这一点。
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:然后是 OrderErrors 列表:
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.我需要在 orderErrors 像客户列表中的 OrderId 上方一样填充之后进行更新。
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; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.