Since your type must be used by more than one method (it must be created by one method and read by another), it should not use an anonymous type. Just create a simple type:
public class TombstoneNursingSchool { public Tombstone Tombstone { get; set; } public NursingSchool NursingSchool { get; set; } }
create it like this:
var personalInfoQuery = from t in crnnsupContext.Tombstones.Include("ProvState") join n in crnnsupContext.NursingSchools on t.NursingSchool equals n.SchoolID where t.RegNumber == _username select new TombstoneNursingSchool { Tombstone = t, NursingSchool = n };
create a list like this:
List<TombstoneNursingSchool> personalInfoResult = personalInfoQuery.ToList();
put it in the cache as follows:
Cache.Insert("personalInfo", personalInfoQuery.ToList())
pull it out of the cache and read it as follows:
foreach(var tn in (List<TombstoneNursingSchool>)Cache["personalInfo"]) {
Anonymous types are convenient in one method, but they are not suitable for every situation. Don't be afraid to make a named type if you need one.
source share