简体   繁体   English

Linq 查询在代码中引发超时,但在 LinqPad 上工作正常

[英]Linq Query Throws Timeout in code but Works fine on LinqPad

When I run the Linq query on LinqPad it takes only 4-5 seconds to return 5000 rows but when I run that same query in my code using Entity Framework it throws timeout.当我在 LinqPad 上运行 Linq 查询时,只需 4-5 秒即可返回 5000 行,但是当我使用实体框架在代码中运行相同的查询时,它会引发超时。

What could be the possible issues?可能的问题是什么?

Thanks in advance.提前致谢。

Below is the query:以下是查询:

var resultSale =           
    from product in Products
    join productInfo in ProductInfoSummaries on product.ID equals productInfo.ProductID
    join productDetail in ProductDetails on new { Id = product.ID, storeId = product.CreatedInStore } equals new { Id = productDetail.ProductID, storeId = productDetail.StoreID }
    
    join productInventoryOtherStore in InventoryOtherStores on product.ID equals productInventoryOtherStore.ProductID
    into productInventories
    from productInventoryOtherStore in productInventories.DefaultIfEmpty()
    
    join saleLine in SaleLines on productDetail.ID equals saleLine.ArtikkelNr
    join sales in Sales on saleLine.OrderID equals sales.ID
    
    where saleLine.ArtikkelNr != null
    && saleLine.DatoTid >= new DateTime(2018, 01, 01)
    && saleLine.DatoTid <= new DateTime(2019,11,21)
    && sales.StoreID == 14
    && (sales.OrderType == 1 || sales.OrderType == 2 || sales.OrderType == 4 || sales.OrderType == 6)
    && productDetail.SupplierProductNo != null
    && productDetail.Deleted == null
    && (productInfo.Inactive == null || productInfo.Inactive == false)
    && (product.CreatedInStore == 14 || product.CreatedInStore == 0 || product.CreatedInStore == null)
    
    group new { saleLine.AntallEnheter, sales.OrderType } by new { product.ID, productInventoryOtherStore.Amount } into g

    select new ProductSaleSummaryVM
    {
        ID = g.Key.ID,
        Inventory = (double)g.Key.Amount,
        TotalSold = g.Sum(x => x.OrderType !=4 ? x.AntallEnheter : 0) ?? 0,
        TotalWastage = g.Sum(x => x.OrderType ==4 ?  x.AntallEnheter : 0) ?? 0,
        TotalOrderedQty = 0
    };
    
var resultSupplierOrder = 
    from supplierOrderLine in SupplierOrderLines
    join supplierOrder in SupplierOrders on supplierOrderLine.SupplierOrderID equals supplierOrder.ID
    where supplierOrderLine.Deleted == null
    && supplierOrder.Status != 1
    && supplierOrder.StoreID == 14
    group supplierOrderLine by supplierOrderLine.ProductID into g
    select new ProductOrderDetailsVM
    {
        ID = g.Key,
        TotalOrderedQty = (double)g.Sum(x => x.ConsumerQuantity - x.QuantiyReceived)
    };
    
var r =   
    (from resSale in resultSale
    join resSupplierOrder in resultSupplierOrder on resSale.ID equals resSupplierOrder.ID
    into resSupplierOrders
    from resSupplierOrder in resSupplierOrders.DefaultIfEmpty()
    orderby resSale.ID
    select new ProductSaleSummaryVM
    {
        ID = resSale.ID,
        Inventory = resSale.Inventory,
        TotalSold = resSale.TotalSold,
        TotalWastage = resSale.TotalWastage,
        TotalOrderedQty = resSupplierOrder.TotalOrderedQty ?? 0
    })
    .Where(x => x.Inventory +  x.TotalOrderedQty < x.TotalSold);
            
r.Dump();

Try using entity framework within linqpad see if it gives you any clues.尝试在 linqpad 中使用实体框架,看看它是否为您提供任何线索。 see this link on how to use entity framework in linqpad请参阅此链接以了解如何在 linqpad 中使用实体框架

https://www.linqpad.net/EntityFramework.aspx https://www.linqpad.net/EntityFramework.aspx

This behaviour could be related to parameter sniffing - check this article for details此行为可能与参数嗅探有关 - 请查看本文了解详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM