Telerik Kendo UI MVC 网格数据源结果到对象列表
Telerik Kendo UI MVC Grid datasourceresult to list of objects
Telerik Kendo UI MVC 网格填充了来自 T1ViewModel 的数据(从 DB1 中的 T1 和 DB2 中的 T2 连接,其中 T1 和 T2 是表,DB1 和 DB2 是数据库)。
T1 有一个 ShopID 列,并且 ShopName 列存在于 DB2 的 T2 中。我在两个上下文中使用 EntityFramwork。
正在将 T1 数据加载到内存中,然后与 T2 连接。
由于我每页只显示 10 条记录,T1 和 T2 有数百万条记录,并且这种连接发生在影响性能的每个页面更改上。
然后我用DataSourceRequest过滤了T1记录。
public ActionResult T1_Read([DataSourceRequest] DataSourceRequest request)
{
using(var context = new DB1Context())
{//Get requested records from T1 into T1ViewModel which has shop name property
DataSourceResult result = context.T1.select(r => new T1ViewModel{ShopId = r.ShopID.....}).ToDataSourceResult(request)
}
IEnumerable<T1ViewModel> T1RecordsFiltered = result.data;
var ShopIds = T1RecordsFiltered.Select(T => T.ShopID);
using(var context = new DB2Context())
{//Get the T2 records that has matching ShopId in the list(ShopIds)
var T2RecordsFiltered = context.T2.Where(T => ShopIds.Contains(T.ShopID)).ToList();
}
var t1ViewModel = from t1rf in T1RecordsFiltered
join t2rf in T2RecordsFiltered on t1rf.ShopID equals t2rf.ShopID into t2rfGroup
from t2rfg in t2rfGroup.DefaultIfEmpty()
select new { t1rf, t2rfg };
var t1ViewModelDetails = t1ViewModel.Select(t =>
{
t.t1rf.ShopName = t.t2rfg.ShopName;
return t.t1rf;
});
result.data = t1ViewModelDetails;
return Json(result, JsonRequestBehavior.AllowGet);
}
现在的问题是
我的网格也是可分组的。但是当我使用该功能时 result.data 有 AggregateFunctionsGroup 列表。所以我无法将 result.data 转换为 T1ViewModel 列表。
这是正确的做法吗?有没有更好的方法?
找到解决方案。我没有依赖 dataSourceResult 的数据,而是使用 ToDataSourceResult 重载方法来创建 t1ViewModels 列表并修改 tViewModels 以更新 shopName,如下所示。
public ActionResult T1_Read([DataSourceRequest] DataSourceRequest request)
{
List<T1ViewModel> t1ViewModels = new List<T1ViewModel>();
DataSourceResult result;
using (var context = new DB1Context())
{//Get requested records from T1 into T1ViewModel which has shop name property
result = context.T1.ToDataSourceResult(request, t =>
{
T1ViewModel tvm = T1ViewModel.FromModel(t);
t1ViewModels.Add(tvm);
return tvm;
});
}
var ShopIds = t1ViewModels.Select(T => T.ShopID);
using (var context = new DB2Context())
{//Get the T2 records that has matching ShopId in the list(ShopIds)
var T2RecordsFiltered = context.T2.Where(T => ShopIds.Contains(T.ShopID)).ToList();
}
var t1ViewModelDetails = from t1vm in t1ViewModels
join t2rf in T2RecordsFiltered on t1vm.ShopID equals t2rf.ShopID into t2rfGroup
from t2rfg in t2rfGroup.DefaultIfEmpty()
select new { t1vm, t2rfg };
t1ViewModelDetails.Select(t =>
{
t.t1vm.ShopName = t.t2rfg.ShopName;
return t.t1vm;
});
return Json(result, JsonRequestBehavior.AllowGet);
}
Telerik Kendo UI MVC 网格填充了来自 T1ViewModel 的数据(从 DB1 中的 T1 和 DB2 中的 T2 连接,其中 T1 和 T2 是表,DB1 和 DB2 是数据库)。
T1 有一个 ShopID 列,并且 ShopName 列存在于 DB2 的 T2 中。我在两个上下文中使用 EntityFramwork。
正在将 T1 数据加载到内存中,然后与 T2 连接。 由于我每页只显示 10 条记录,T1 和 T2 有数百万条记录,并且这种连接发生在影响性能的每个页面更改上。
然后我用DataSourceRequest过滤了T1记录。
public ActionResult T1_Read([DataSourceRequest] DataSourceRequest request)
{
using(var context = new DB1Context())
{//Get requested records from T1 into T1ViewModel which has shop name property
DataSourceResult result = context.T1.select(r => new T1ViewModel{ShopId = r.ShopID.....}).ToDataSourceResult(request)
}
IEnumerable<T1ViewModel> T1RecordsFiltered = result.data;
var ShopIds = T1RecordsFiltered.Select(T => T.ShopID);
using(var context = new DB2Context())
{//Get the T2 records that has matching ShopId in the list(ShopIds)
var T2RecordsFiltered = context.T2.Where(T => ShopIds.Contains(T.ShopID)).ToList();
}
var t1ViewModel = from t1rf in T1RecordsFiltered
join t2rf in T2RecordsFiltered on t1rf.ShopID equals t2rf.ShopID into t2rfGroup
from t2rfg in t2rfGroup.DefaultIfEmpty()
select new { t1rf, t2rfg };
var t1ViewModelDetails = t1ViewModel.Select(t =>
{
t.t1rf.ShopName = t.t2rfg.ShopName;
return t.t1rf;
});
result.data = t1ViewModelDetails;
return Json(result, JsonRequestBehavior.AllowGet);
}
现在的问题是 我的网格也是可分组的。但是当我使用该功能时 result.data 有 AggregateFunctionsGroup 列表。所以我无法将 result.data 转换为 T1ViewModel 列表。 这是正确的做法吗?有没有更好的方法?
找到解决方案。我没有依赖 dataSourceResult 的数据,而是使用 ToDataSourceResult 重载方法来创建 t1ViewModels 列表并修改 tViewModels 以更新 shopName,如下所示。
public ActionResult T1_Read([DataSourceRequest] DataSourceRequest request)
{
List<T1ViewModel> t1ViewModels = new List<T1ViewModel>();
DataSourceResult result;
using (var context = new DB1Context())
{//Get requested records from T1 into T1ViewModel which has shop name property
result = context.T1.ToDataSourceResult(request, t =>
{
T1ViewModel tvm = T1ViewModel.FromModel(t);
t1ViewModels.Add(tvm);
return tvm;
});
}
var ShopIds = t1ViewModels.Select(T => T.ShopID);
using (var context = new DB2Context())
{//Get the T2 records that has matching ShopId in the list(ShopIds)
var T2RecordsFiltered = context.T2.Where(T => ShopIds.Contains(T.ShopID)).ToList();
}
var t1ViewModelDetails = from t1vm in t1ViewModels
join t2rf in T2RecordsFiltered on t1vm.ShopID equals t2rf.ShopID into t2rfGroup
from t2rfg in t2rfGroup.DefaultIfEmpty()
select new { t1vm, t2rfg };
t1ViewModelDetails.Select(t =>
{
t.t1vm.ShopName = t.t2rfg.ShopName;
return t.t1vm;
});
return Json(result, JsonRequestBehavior.AllowGet);
}