views:

444

answers:

2

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;


}
+1  A: 

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)
}
Philippe Leybaert
+1  A: 

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;
John Saunders
Cheers not quite sure what to do with it now I have it but at least it loads :l
Chris M
What did you want to do with it?
John Saunders
I'm trying to pull it out so i can then (a lot like your example) requery it and pull out a value or just output all the values. I cant get to the data.I tried accessing it IQueryable appCommonRar = (IQueryable)HttpContext.Current.Application["commonRAR"];But as you can probably guess I'm pretty new to all this LINQ/.net3.5/MVC stuff and am stabbing wildly in the dark.
Chris M
>> http://stackoverflow.com/questions/1136705/dealing-with-linq-httpcontext-application-webservices
Chris M