简体   繁体   English

如何在应用程序状态(C#ASP.net MVC)中存储列表〜数组

[英]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. 最好的办法是将其IEnumerableIEnumerable ,但这是无类型的可枚举接口。

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.

相关问题 ASP.NET MVC 4 C#HttpPostedFileBase,如何存储文件 - ASP.NET MVC 4 C# HttpPostedFileBase, How do I Store File 如何在ASP.NET MVC 3应用程序中实现共享状态? - How do I implement shared state in an ASP.NET MVC 3 application? 商店标签如何进入C#asp.net列表应用程序? - How can a store tag's into a list C# asp.net application? 如何将 C# 属性应用于 ASP.NET MVC Web 应用程序的列表内的类型 - How to apply C# Attributes to the type inside of a List for ASP.NET MVC Web Application 如何将文件列表从磁盘存储到C#ASP.NET中的数组中? - How to store a list of files from disk into an array in C# ASP.NET? C#/ ASP.NET MVC:如果我知道用户名和密码,如何在其他Web应用程序中注册/登录? - C#/ASP.NET MVC: how to register/log in a different web application if I know username & password? 如何在 asp.net 核心 mvc ZD7EFA19FBE7D39372FD5ADB60242 中将列表 object 转换为 Viewmodel object? - How can I convert List object to Viewmodel object in asp.net core mvc C#? C#ASP.NET MVC项目和Google Maps:如何使用C#List &lt;&gt;中的值填充JavaScript数组 - C# ASP.NET MVC Project and Google Maps: How to fill JavaScript array with values from C# List<> asp.net c#MVC:如何在没有ViewState的情况下生活? - asp.net c# MVC: How do I live without ViewState? 如何使用c#枚举在ASP.Net MVC中创建一组多选复选框? - How do I make a group of multi-select checkboxes in ASP.Net MVC with c# enum?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM