Dealing with LINQ / HttpContext.Application & WebS

2019-09-04 14:55发布

问题:

I have the following function + code to parse and query a webservice request then in theory store the results in the application (as it only needs to refresh once a day, which luckily is when the application refreshes).

//tocommondata is a reference to the common stuff in the webservice
var dCountries = toCommonData.PropertyCountries; //KeyValuePair
var dRegions = toCommonData.Regions;//Array
var dAreas = toCommonData.Areas;//Array


var commonDAT = (from c in dCountries
                 join r in dRegions on c.Key equals r.CountryCode
                 join a in dAreas on r.Id equals a.RegionId
                 join p in dResorts on a.Id equals p.AreaId
                 select new CommonSave
                 {
                   Key = c.Key,
                   Value = c.Value,
                   Id = r.Id,
                   Name = r.Name,
                   dAreasID = a.Id,
                   dAreasName = a.Name,
                 }
                 ).ToList().AsQueryable();


HttpContext.Current.Application["commonDAT"] = commonDAT;

THIS Bit Works Fine

foreach (var item in commonDAT)
{
  Response.Write(item.value);

}

**Ideally I want to then pull it out of the appmemory so I can then access the data, which is where I've tried various (probably stupid) methods to use the information. Just pulling it out is causing me issues :o( **

//This Seems to kinda work for at least grabbing it (I'm probably doing this REALLY wrong).
IQueryable appCommonRar = (IQueryable)HttpContext.Current.Application["commonDAT"];


Answer as Marked(remove the .AsQueryable() ) Quick Example

Just to make this a verbose answer, a quick way to re-query and display a resultset...

List<CommonSave> appcommon = (List<CommonSave>)HttpContext.Current.Application["commonDAT"];

Response.Write(appcommon.Count()); // Number of responses

var texst = (from xs in appcommon
             where xs.Key == "GBR"
             select xs.dAreasName
             );

foreach (var item in texst)
{
   Response.Write("<br><b>"+item.ToString()+"<b><br>");
}

Hopefully of use to someone.

回答1:

If you're going to query it more than once, then why not just leave it as a list? The type will be List<CommonSave>.