public class Order {
public long Id { get; set; }
public string CustomerName { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public bool Shipped { get; set; }
public IEnumerable<OrderLine> Lines { get; set; }
}
public class OrderLine
{
public long Id { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
public int Quantity { get; set; }
public long OrderId { get; set; }
public Order Order { get; set; }
}
var products = this.productRepository.Products;
Order order = id == 0 ? new Order() : this.ordersRepository.GetOrder(id);
IDictionary<int, OrderLine> linesMap = order.Lines?.ToDictionary(l => l.ProductId) ?? new Dictionary<int, OrderLine>();
ViewBag.Lines = products.Select(p => linesMap.ContainsKey(p.Id)? linesMap[p.Id]: new OrderLine { Product = p, ProductId = p.Id, Quantity = 0 });
linesMap[p.Id] 也就是OrderLine,它的Product属性是如何被关联到数据库的内容的
问题写的不清楚,建议把问题弄详细点,Order类也没有看到,
linesMap[p.Id] 也就是OrderLine,它的Product属性是如何被关联到数据库的内容的,new OrderLine { Product = p, ProductId = p.Id, Quantity = 0 });自己new的就要手动赋值,从DB查出的自动关联了