【问题标题】:Covert Collection of Objects to Collection of Dictionary<string, object>将对象集合隐蔽到字典集合<string, object>
【发布时间】:2014-10-26 17:47:11
【问题描述】:

我正在从 RIA 服务获取大量数据。返回类型有一组对象,如 RouteA、HistroyRouteA。 HistroyLogRouteA 都有不同年份的记录,具有相同的唯一键。

我必须将此数据动态绑定到 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 行这样的小记录。但是当它尝试转换所有数据时,它变得很慢。和屏幕崩溃。我认为这是因为反射。还有其他方法可以将我的获取数据转换为字典吗?意味着我可以将我的表映射到实体框架中的字典,或者 Linq 可以为我执行此操作,而不会让我的代码变慢等。

我的实体使用 EF 6 进行映射,并且我正在使用 Deart oracle 连接器。

【问题讨论】:

    标签: c silverlight telerik wcf-ria-services devart


    【解决方案1】:

    由于反射,它变得非常慢,所以我在 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);
    

    【讨论】:

      猜你喜欢
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2018-08-13
      相关资源
      最近更新 更多