【问题标题】:How do I store List ~ Array in the Application State (C# ASP.net MVC)如何在应用程序状态中存储 List ~ Array (C# ASP.net MVC)
【发布时间】:2010-11-10 23:27:31
【问题描述】:

我正在尝试将一些数据粘贴到应用程序中,这样我就可以构建一个公共字符串 get + linq 查询,以便在需要时提取我想要的位。

我只是在努力存储它,然后我将如何将它拉回来以便我可以查询它......

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;


}

【问题讨论】:

    标签: c# asp.net-mvc .net-3.5 linq-to-objects


    【解决方案1】:

    您可以创建一个匹配数据的类,并返回该类的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;
    

    【讨论】:

    • 欢呼不知道该怎么办现在我有了它,但至少它加载了:l
    • 你想用它做什么?
    • 我正在尝试将其拉出,以便我可以(很像您的示例)重新查询它并拉出一个值或仅输出所有值。我无法获取数据。我尝试访问它 IQueryable appCommonRar = (IQueryable)HttpContext.Current.Application["commonRAR"];但正如你可能猜到的那样,我对所有这些 LINQ/.net3.5/MVC 的东西都很陌生,并且在黑暗中疯狂地刺伤。
    【解决方案2】:

    您存储的集合是匿名类型的枚举。从 Application[] 存储中取回项目时,您必须将其转换为 IEnumerable<TYPE>,但由于它是匿名的,因此您不能这样做。

    你能做的最好的是将它转换为IEnumerable,但那是无类型的可枚举接口。

    例如:

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多