2014-10-26 21 views
-1

我正在從RIA服務中獲取大量數據。返回類型有一組對象,如RouteA,HistroyRouteA。 HistroyLogRouteA都具有相同唯一密鑰的不同年份的記錄。隱藏收集字典對象的對象<string,object>

我必須將這些數據動態綁定到RadGridView。總是我有未知的列結果。 爲此,我跟着

http://blogs.telerik.com/vladimirenchev/posts/11-09-28/dynamic-binding-for-your-silverlight-applications.aspx

http://www.telerik.com/forums/rowdetailstemplate-binding-with-dynamic-data

,及代碼建立我的數據收集:

private void OnShowPreviousYear(object parameter) 
    { 
     GridViewHeaderCell cell = parameter as GridViewHeaderCell; 
     var head = cell.Column.Header; 
     this.context.Load<Route>(this.context.GetRoutesQuery(), LoadBehavior.MergeIntoCurrent, OnRouteHistoryLoadComplete, null); 
    } 

    private void OnRouteHistoryLoadComplete(LoadOperation<Route> lo) 
    { 
     object ro = null; 
     if (lo.Entities != null) 
     { 

      this.context.Load<Routeshistory>(this.context.GetRouteshistoriesQuery(), LoadBehavior.MergeIntoCurrent, (lp) => 
      { 
       Route recent = lo.Entities.FirstOrDefault(); 
       int year =(int)recent.Hpmsyear-1; 
       var rows = this.context.Routes.Join(this.context.Routeshistories, 
        r => r.Routeid.ToString(), 
        h => h.Routeid.ToString(), 
        (r, h) => new { r, h });//.Where(t => t.r.Routeid == t.h.Routeid); 


       RoutesGridData = new ObservableCollection<DataRow>(); 
       int count = 0;      
       foreach (var tmpR in rows) 
       { 
        //Debug.WriteLine(tmpR.r.Routeid + " -- " + tmpR.h.Routeid); 
        if (count < 50) 
        { 
         DataRow row = new DataRow(); 

         if (tmpR.r is Route) 
         { 
          Type type = tmpR.r.GetType(); 
          foreach (PropertyInfo info in type.GetProperties()) 
          { 
           // Debug.WriteLine(info.Name + "--- NAME OF PRR"); 
           var val = info.GetValue(tmpR.r, null); 
           if (!info.Name.Equals("EntityConflict") 
            && !info.Name.Equals("ValidationErrors") 
            && !info.Name.Equals("HasValidationErrors") 
            && !info.Name.Equals("EntityState") 
            && !info.Name.Equals("HasChanges") 
            && !info.Name.Equals("IsReadOnly") 
            && !info.Name.Equals("EntityActions")) 
           { 
            row[info.Name] = val; 
           } 
          } 
         } 
         // other tables... 
         RoutesGridData.Add(row); 

        } 
        count++;       
       } 

      }, null); 
     } 
    // var b = ro; 
    } 

此代碼工作正常,像50行的小記錄。但是當它試圖轉換所有數據時變慢。和屏幕崩潰。我認爲這是因爲反思。有沒有其他方法可以將我的提取數據轉換爲Dictionary?意味着我可以映射我的表在實體框架或LINQ到字典中能爲我做到這一點沒有得到我的代碼慢等

我的實體與EF 6 &映射餘米使用Deart oracle的連接器。

回答

0

由於反思,它變得非常緩慢,所以我在Linq查詢期間做了一段時間的工作,我有什麼數據與我在一起。

var rowss = this.context.Routes.Join(this.context.Routeshistories, 
         r => r.Routeid, 
         h => h.Routeid, 
         (r, h) => new DataRow(
          (from x in r.GetType().GetProperties() select x).Where(x => x.Name != "EntityConflict" 
           && x.Name != "ValidationErrors" 
           && x.Name != "HasValidationErrors" 
           && x.Name != "HasChanges" 
           && x.Name != "EntityState" 
           && x.Name != "IsReadOnly" 
        && x.Name != "EntityActions") 
        .ToDictionary(x => x.Name, x => (x.GetGetMethod().Invoke(r, null) == null ? "" : x.GetGetMethod().Invoke(r, null))), 
          (from x in h.GetType().GetProperties() select x).Where(x => x.Name == head) 
          .ToDictionary(x => x.Name + "-" + year.ToString(), x => (x.GetGetMethod().Invoke(h, null) == null ? "" : x.GetGetMethod().Invoke(h, null)))) 
         );// , new EqualityComparerString() 

RoutesGridData = new ObservableCollection<DataRow>(rowss);