So, this may seem like an edge case, but I'm simply wondering if this is possible. What I have is a combination of a static set and deferred set wrapped in an IEnumerable
- for example:
public IEnumerable<T> MakeMyQuery<T>()
{
// returns a List<T>
var someStaticData = LoadMyStaticDataFromDatabase();
// Returns IEnumerable<T>, but from another resource
var deferredQuery = CreateADeferredQueryUsingYieldReturn();
return someStaticData.Concat(deferredQuery);
}
So what happens here is that when I call .Take(someNumber)
on my enumerable, it will return elements from my static Data first before attempting to evaluate the deferred component - effectively, I have "hidden" some potentially time consuming producing tasks behind the enumerable such that if I never need to get those elements, they actually never get evaluated because of the deferred nature of LINQ.
However, I don't think it's possible to cache this query for later use (I don't believe the state of the Iterator will be kept in cache, right?) Or is there an easy way to do this without enumerating the results to save?
Ideally, my flow would be like so:
public List<T> SomeMethod<T>(int numberOfGuys)
{
IEnumerable<T> query = null;
if(// Is in Cache)
query = Cache["MyQuery"];
else
{
query = MakeMyQuery();
Cache["MyQuery"] = query;
}
return query.Take(numberOfGuys).ToList();
}
So I can re-use the same query over and over to request data, but potentially never have to requery the DB. Is there a way to do this?
LoadMyStaticDataFromDatabase
? That seems independent of how you concatenate it with other data. – Criminology