假设我有一个实体:
public class Table {
[Key]
[Column]
public long TableId { get; set; }
[ForeignKey("TableId")]
[JsonIgnore]
[Association("Diners", "TableId", "TableId")]
public List<Diner> DinersAtTable { get; set; }
[NotMapped]
public int Total { get; set; }
}
public class Diner {
[Key]
[Column]
public long DinerId { get; set; }
public long TableId { get; set; }
}
public class Item {
[Key]
[Column]
public long ItemId { get; set; }
public long DinerId { get; set; }
public long Price { get; set; }
}
我想用食客订购的所有物品的价格总和填充Total,但是由于我对商品本身并不真正感兴趣,因此我想在查询中填充Total,这样我就可以表实体。
所以,我想要类似的东西:
var query = ctx.Tables.Include(t => t.DinersAtTable)
.Include(t = t.DinersAtTable.Select(d => d.Include(Items)))
.AssignToUnmapped(t => t.Total = t.Diners.Items.Sum()).Select(t => t.TableId, t.Total)
很显然,AssignToUnmapped和最终的Select是我不确定如何实现的。知道我该怎么做吗?