繁体   English   中英

Any() Linq 方法对同一个 object 进行多个查询。 C# EF CORE

[英]Any() Linq Method making several queries on the same object. C# EF CORE

我进行了几次搜索,但我仍在为此苦苦挣扎。 我想在 EF 内核上执行此查询,使用 sql 服务器 Exists():

问题是,在 sql 服务器中,下面的查询需要大约 2 秒才能执行。 在 C# 上,使用 Any() 方法需要超过 1 分钟。 它对 HutOperationHistory 和 HutPropertyValue 表进行多次查询。

// 需要 1-2 秒

select * From Hut h where exists(
    select 1 from HutOperationHistory 
             where HutId = h.HutId and HutOperationId = 4
)
and exists(
    select 1 from HutPropertyValue 
             where HutId = h.HutId and Value = 'HUT_SUPPLIER_PROPERTY_NAME'
)
and exists(
    select 1 from HutPropertyValue 
             where HutId = h.HutId and Value = 'BUY_ORDER_PROPERTY_NAME'
)
and exists(
    select 1 from HutPropertyValue 
             where HutId = h.HutId 
             and Value = 'WORK_ORDER_PROPERTY_NAME'
)

我在 linq 上做到了,它是这样的:

// takes more then 1 minute



  var huts = coreDbContext.Huts
        .Include(x => x.PropertyValuesList).ThenInclude(y => y.HutProperty)
        .Include(x => x.OperationHistoriesList)
        .Where(
            x => 
             x.OperationHistoriesList.Any(
                z => ( z.HutOperation.hutOperationId = 4 )
            )
            && x.PropertyValuesList.Any(b => 
                    b.Value.Equals(hutSupplierPropName, StringComparison.CurrentCultureIgnoreCase)                  
                )
            && x.PropertyValuesList.Any(b =>
                b.Value.Equals(buyOrderPropName, StringComparison.CurrentCultureIgnoreCase)             
            )
            && x.PropertyValuesList.Any(b =>
                b.Value.Equals(workOrderPropName, StringComparison.CurrentCultureIgnoreCase)                
            )
        )
        .OrderBy(x => x.Order)
        .ToList();

有什么我可以做的让它更快吗?

提前谢谢!

尝试使用 Count() > 0 而不是 Any()。过去我使用此方法获得了性能提升

暂无
暂无

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

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