I have set of ‘codes’ Z that are valid in a certain time period.
Since I need them a lot of times in a large loop (million+) and every time I have to lookup the corresponding code I cache them in a List<>. After finding the correct codes, i’m inserting (using SqlBulkCopy) a million rows.
I lookup the id with the following code (l_z is a List<T>)
var z_fk = (from z in l_z
where z.CODE == lookupCode &&
z.VALIDFROM <= lookupDate &&
z.VALIDUNTIL >= lookupDate
select z.id).SingleOrDefault();
In other situations I have used a Dictionary with superb performance, but in those cases I only had to lookup the id based on the code.
But now with searching on the combination of fields, I am stuck.
Any ideas? Thanks in advance.
Create a Dictionary that stores a List of items per lookup code –
Dictionary<string, List<Code>>(assuming that lookup code is a string and the objects are of type Code).Then when you need to query based on
lookupDate, you can run your query directly off ofdict[lookupCode]:Then just make sure that whenever you have a new Code object, that it gets added to the
List<Code>collection in thedictcorresponding to thelookupCode(and if one doesn’t exist, then create it).