Search code examples
c#linqienumerableiqueryableresx

LINQ to resx file?


I'm trying to build a way to get the key for a given piece of text in a given .resx file at runtime.

Currently, I can open and read the file (Using ResXResourceReader) but I have to use a foreach to go over the entire file.

This could be a performance issue, as some of our .resx files are fairly large (in the order of 2000 strings) and we may be doing this frequently.

I'd like to use LINQ to Objects to query this, as I assume the where method is relatively optimized, however I've been unable to do so. The ResXResourceReader class has two methods AsQueryable() and GetEnumerator(), but neither allow LINQ against their result (so from n in reader.AsQueryable() where fails).

How can I LINQ against something provided by the ResXResourceReader, or is it even worth the time?


Solution

  • The following code returns an IEnumerable object that allows you to use LINQ to query the contents of a resource file:

    ResXResourceReader reader = new ResXResourceReader(myfile);
    IEnumerable<DictionaryEntry> enumerator = reader.OfType<DictionaryEntry>();