简体   繁体   中英

How do I store List ~ Array in the Application State (C# ASP.net MVC)

I'm trying to stick some data into the app so I can then build a public string get + linq query to pull out just the bits I want when I need them.

I'm just struggling to store it and then how I'd go about pulling it back out so I can query against it...

public void CommonDatatoApp() 
{
  CDataResponse cCommonData = this.GatewayReference.GetCommonData();
  var dCountries = cCommonData.PropertyCountries; //KeyValue
  var dRegions = cCommonData.Regions; //Array
  var dAreas = cCommonData.Areas; //Array
  var dResorts = cCommonData.Resorts; //Array

  var commonRAR = (from c in dCountries
                   join r in dRegions on c.Key equals r.CountryCd
                   join a in dAreas on r.Id equals a.RegionId
                   select new { c.Key, c.Value, r.Id, r.Name, dAreasID = a.Id, dAreasIDName = a.Name}
                  );


  HttpContext.Current.Application["commonData"] = commonRAR;


}

The collection you're storing is an enumeration of anonymous types. When retrieving the item back from the Application[] store, you would have to cast it to IEnumerable<TYPE> , but since it's anonymous, you can't do that.

The best you can do is cast it to IEnumerable , but that's the untyped enumerable interface.

For example:

IEnumerable myList = (IEnumerable) HttpContext.Current.Application["commonData"];

foreach (object obj in myList)
{
   // do something with obj (but that will be hard, because it is of 
   // an anonymous type)
}

You can create a class that matches the data, and return an IQueryable<T> of that class:

public class SaveMe { 
   public string Key {get;set}
   public string Value {get;set;}
   public int Id {get;set;}
   public string Name {get;set;}
   public int dAreasID {get;set;}
   public string dAreasIDName {get;set;}
}

  var commonRAR = (from c in dCountries
                   join r in dRegions on c.Key equals r.CountryCd
                   join a in dAreas on r.Id equals a.RegionId
                   select new SaveMe {
                      Key= c.Key, 
                      Value = c.Value, 
                      Id = r.Id, 
                      Name = r.Name, 
                      dAreasID = a.Id, 
                      dAreasIDName = a.Name
                    }
                  );


  HttpContext.Current.Application["commonData"] = commonRAR;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM