[英]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. 我正在尝试将一些数据粘贴到应用程序中,以便随后我可以构建一个公共字符串get + linq查询以仅在需要它们时提取我想要的位。
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. 从Application []存储中检索项目时,必须将其
IEnumerable<TYPE>
为IEnumerable<TYPE>
,但是由于它是匿名的,因此您不能这样做。
The best you can do is cast it to IEnumerable
, but that's the untyped enumerable interface. 最好的办法是将其
IEnumerable
为IEnumerable
,但这是无类型的可枚举接口。
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: 您可以创建一个与数据匹配的类,并返回该类的
IQueryable<T>
:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.