首页 新闻 会员 周边

问个ef core 投影问题

0
悬赏园豆:50 [已解决问题] 解决于 2021-01-26 13:20

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属性是如何被关联到数据库的内容的

leyestd的主页 leyestd | 初学一级 | 园豆:149
提问于:2021-01-20 14:31
< >
分享
最佳答案
0

问题写的不清楚,建议把问题弄详细点,Order类也没有看到,

收获园豆:50
godcloudy | 初学一级 |园豆:18 | 2021-01-20 17:27

linesMap[p.Id] 也就是OrderLine,它的Product属性是如何被关联到数据库的内容的,new OrderLine { Product = p, ProductId = p.Id, Quantity = 0 });自己new的就要手动赋值,从DB查出的自动关联了

leyestd | 园豆:149 (初学一级) | 2021-01-20 20:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册