LINQ to resx file?
I am trying to create a way to get the key for a given piece of text in a given file .resx
at runtime.
Currently I can open and read the file (using ResXResourceReader
), but I have to use foreach
to move around the whole file.
This can be a performance issue as some of our files are .resx
quite large (on the order of 2000 lines) and we do this a lot.
I would like to use LINQ to Objects to query as I assume the method is where
relatively optimized, however I was unable to do so. The class ResXResourceReader
has two methods AsQueryable()
and GetEnumerator()
, but neither of them allows LINQ on their result (therefore from n in reader.AsQueryable() where
does not work).
How can I LINQ from something provided ResXResourceReader
, or is it even worth the time?
source to share
Did you think you were just doing preprocessing?
You can load the Resx file, then iterate over the key-value pairs, and then store them in the hash table of the file (i.e. the key in the hash table is actually the resource value, and the value in the hash table is the resource key). Then the translations of the value-> key will be fast. The only problem is how you handle duplicates (multiple resources with the same value).
source to share
It's not worth your time. LINQ doesn't have a way to find your .resx file more efficiently than you could. Depending on your memory usage and whether caching data makes sense, you would probably be better off reading the entire file in Dictionary<TKey,TValue>
and searching that way.
You could, of course, create your own tuplet object to hold the key / value pair and read the entire file in a list of them, and then allow the LINQ objects to execute your query, but that won't be as fast as Dictionary
it would require additional overhead expenses.
source to share