Question

I'm caching a whole bunch of static metadata in my app at startup. It's from a db and there are tons of Foreign Key relationships. I'm looking into the best way of modelling them. I've just started off with LINQ.

It's easy for me to declare a class

public class AllData {
    public static IQueryable<libm_ColPurpose> IQ_libm_ColPurpose = libm_ColPurpose.All();
    public static IQueryable<libm_ColType> IQ_libm_ColType = libm_ColType.All();
...

(I'm using SubSonic 3 to generate my classes, but that's beside the point). Then I can use the IQueryable<T> members to get access to anything I want, for example:

libm_ColType ct = AllData.IQ_libm_ColType.SingleOrDefault(x => x.ColTypeStr == this.DefaultJetColTypeStr);

Prior to using IQueryable I was using Dictionaries to store FK relationships, so to mimic the above code I'd code the following from a preexisting List<libm_ColType> list

Dictionary<string, libm_ColType> colTypeByColTypeStr = new Dictionary<string, libm_ColType>();
foreach (libm_ColType x in list) { rtn.Add(x.ColTypeStr, x); }

and then I could use

libm_ColType ct = colTypeByColTypeStr[this.DefaultJetColTypeStr];

OK, so finally we get to the question !

The Dictionary lookup by ID is extremely efficient, however the IQueryable solution is far more flexible and elegant.

I'm wondering how much of a performance hit I'm going to get using IQueryable. I suspect I am doing a linear scan of the list each time I call it, and that's really gonna add up over repeat calls if there are a lot of records involved. It woul be great if I could identify unique-valued columns and have a hashtable generated and cached after the first lookup, but I suspect this is not gonna be part of the offering.

This is a bit of a dealbreaker for me regarding using LINQ.

Note (I'll repeat it again) that I'm NOT pulling data from a database, it's already in memory and I'm querying it there, so I'm only interesting in looking up the in-memory IQueryable<T>.

Was it helpful?

Solution

IQueryable represents a collection in a data-store, so you probably don't have the collections in memory. If you explicitly want in-memory collections, then I would go back to your dictionaries. Remember, this doesn't prevent you from using LINQ queries over the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top