我有两个表:客户和订单。客户可以参考如下订单:
[Reference]
public List<Order> Orders { get; set; }
Order类有一个属性Deleted
。我想加载所有客户(或子集),并包含订单,但不包括Deleted = true的订单。可以使用LoadSelect方法完成此操作,还是建议的方法是什么?
大致等于以下SQL的东西:
select * from Customers C
join Orders O on O.CustomerId = C.Id
where O.Deleted = False
这是最好的方式吗?
var orderIds = resp.Customers.Select(q => q.Id).ToList();
var allOrders = Db.Select<Order>(o => orderIds.Contains(o.CustomerId) && !o.Deleted);
foreach (var order in allOrders)
{
var cust = resp.Customers.First(q => q.Id == order.custivityId);
if (cust.Orders == null) cust.Orders = new List<Order>();
cust.Orders.Add(order);
}
答案 0 :(得分:1)
我刚刚添加了一个新的Merge
API in this commit来自动加入父系列及其子引用,这会让这更容易。
使用新API,您可以单独选择客户和订单,并将这些集合合并在一起,例如:
//Select only Customers which have valid orders
var customers = db.Select<Customer>(q =>
q.Join<Order>()
.Where<Order>(o => o.Deleted == false)
.SelectDistinct());
//Select valid orders
var orders = db.Select<Order>(o => o.Deleted == false);
customers.Merge(orders); //merge the results together
customers.PrintDump(); //print the results of the merged collection
此修改可从 v4.0.39 + 获得,现在为available on MyGet。