I am turning some stored procedure to vb.net linq (SQL to linq …. manually) because stored procedure is slow.
I am using linq queries in concurrent threads.
After running a performance analyzing, I find out the linq seems to lock the source collection(like cache.IPMS_TBL_EL_PRICE_COMPONENT in the code period below) when querying.
Is it true? Is there a (not_lock/lock) option for linq?
I really don’t want the collection to be locked. It will slow the multi-thread query.
Thank you very much.
Code period:
see in https://i.stack.imgur.com/YLlI6.jpg or see below
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
You are calling
ToListwhich causes the query to evaluate eagerly (there and then).With
ToList, it will iterate over the result set returning the requested results – this will indeed use the current thread.You can defer evaluation by not calling
ToListand only evaluate when you actually need to iterate over the results.