我正在将一些存储过程转换为vb.net linq(SQL到linq ....手动),因为存储过程很慢。
我在并发线程中使用linq查询。
运行性能分析后,我发现linq似乎在查询时锁定了源集合(如下面的代码周期中的cache.IPMS_TBL_EL_PRICE_COMPONENT)。
这是真的吗? linq有(not_lock / lock)选项吗? 我真的不希望锁定集合。它会减慢多线程查询的速度。
非常感谢。
代码期:
insert0 = (From PPC In cache.IPMS_TBL_EL_PRODUCT_PRICE_COMPONENT_MAPPING
From PC In cache.IPMS_TBL_EL_PRICE_COMPONENT
Join LK In cache.IPMS_TBL_LOOKUP
On PC.Component_Type_Id Equals LK.Lookup_Id
Where (PC.Component_Id = PPC.Component_Id OrElse PC.Component_Type_Id = CC3_ID) _
AndAlso LK.Commodity_Id = ELE_COMMODITY_ID _
AndAlso LK.Lookup_Type.ToLower = PRICE_COMPONENT_TYPE.ToLower _
AndAlso PPC.Product_Id = IN_PRODUCT_ID _
AndAlso PPC.Price_Type_Id = IN_PRICE_TYPE_ID _
AndAlso PC.Is_Deleted = 0 _
AndAlso LK.Lookup_Id > MINUS_HUNDRED _
AndAlso PC.Component_Id > MINUS_HUNDRED _
AndAlso lookupValues.Contains(LK.Lookup_Value.ToLower) _
AndAlso (Not PC.ISO_Id.HasValue OrElse Not deletedISO.Contains(PC.ISO_Id.Value))
Select New PriceComponents() With {.ComponentID = PC.Component_Id,
.ComponentName = PC.Component_Name,
.ComponentTypeID = PC.Component_Type_Id,
.ComponentTypeName = LK.Lookup_Value,
.Sequence = PC.Sequence,
.OrderSequence = orderSequeceDict(LK.Lookup_Value.ToLower),
.IsMTM = PC.Is_MTM,
.UcapUsageFactorUnitPrice = PC.UCAP_Usage_Factor_UnitPrice,
.Percentage = PERCENTAGE}
).OrderBy(Function(e As PriceComponents) e.OrderSequence).ThenBy(Function(e As PriceComponents) e.Sequence) _
.Distinct(New PriceComponentsComparer_PK_9_Fields).ToList
答案 0 :(得分:3)
您正在调用ToList
,这会导致查询急切地评估(然后)。
使用ToList
,它将迭代返回请求结果的结果集 - 这确实会使用当前线程。
您可以通过不调用ToList
来推迟评估,并且仅在您实际需要迭代结果时进行评估。