我有3张桌子。订单,订单行和商店。在Order表中具有StoreId,这是Stores表的外键,在Orderlines表中具有OrderId,是Order表的外键。我只需要获取字段Order.OrderId,Order.WarehouseOrderId,Store.StoreName和每个订单的订单行数。所以目前这是我的实现:
public List<Order> GetOrdersDashboardWithoutStatus1(SearchDashboardCriteria cri)
{
using (var ctx = CreateDbContext())
{
var orders = (from items in ctx.Orders select items);
if (cri.MerchantId != 0)
{
orders = orders.Where(x => x.Store.MerchantId == cri.MerchantId);
}
return orders.Where(x => (DbFunctions.TruncateTime(x.DateImported) >= DbFunctions.TruncateTime(cri.StartDate))
&& (DbFunctions.TruncateTime(x.DateImported) <= DbFunctions.TruncateTime(cri.EndDate)))
.Include(x => x.Store).Include(x => x.OrderLines).ToList();
}
}
尽管当前实现可以正常运行,但是性能却很慢,有时会出现超时错误,因为一条记录可能有30K +订单。
答案 0 :(得分:0)
您可以尝试这样的事情
using (var ctx = CreateDbContext())
{
var orders = (from x in ctx.Orders
where x.Store.MerchantId == cri.MerchantId
&& add your other where condition
select NewDataModel
{
orderId = x.OrderId,
wareHouse = x.WarehouseOrderId,
storeName = x.Store.StoreName
}).ToArray();
}
匿名投影或自定义模型的投影仅检索投影中包含的列。
它将是Select * from Orders ...
而不是Select OrderId,WareHouseOrderId from Orders
如果您的用例支持,也可以尝试分页(Take,skip)
。
Similar answer for anonymous projection