I have a LINQ query that I am having trouble optimizing. It takes about 6 seconds to run.
IList<Note> notes;
data = client.Configuration.Data.OfType<UWData>()
.Where(s => s.ShouldDisplay)
.OrderByDescending(s => s.Id)
.ToList()
.Select(n => (Note)n)
.ToList();
client.Configuration.Data contains hundreds of thousands of data (100,000 items).
client.Configuration.Data is a cache, and I am retrieving data from the cache.
client.Configuration.Data contains 100,000 items, and we have 10 items which is the type of UWData. I want to select the 10 UWData.
var data = client.Configuration.Data.OfType<UWData>().ToList();
data = UWData
.Where(s => s.ShouldDisplay)
.OrderByDescending(s => s.Id)
.ToList()
.Select(n => (Note)n)
.ToList();
The above code also taking a similar time to load. Sometimes it takes around 4 seconds to load (exactly 3,356 ms). Any ideas on how I could speed this up?
entity-frameworktag? Remove the tag and specify in the question that you are running LINQ in-memory query (LINQ to Objects).n log ncomplexity, for that many items should be in the order of tens to a hundreds of milliseconds, not seconds. Use a profiler!Datacache implementation. You use terms like "cache", "loading" which are totally unclear (people are assuming cache to be some sort of list, but then there wouldn't be such thing as loading), so you really need to find out what is causing the performance issue and how/if it can be improved. All that inside "cache" implementation, there is nothing we can't help.