服务器过滤剑道网格时内存不足异常

本文关键字:内存不足 异常 网格 过滤 服务器 | 更新日期: 2023-09-27 18:16:05

我想在剑道网格中显示大数据,使用跳过和取服务器分页和过滤的解决方案。我的控制器和方法是:

public ActionResult LoadStudents(int page, int pageSize, int take, bool? activeOnly)
    {
        var sorterCollection = KendoGridSorterCollection.BuildCollection(Request);
        var filterCollection = KendoGridFilterCollection.BuildCollection(Request);
        ...
        LogUI.Data.kiwiEntities en = new Data.kiwiEntities();
        var result = en.Syslogds.Where(s => el.Contains(s.MsgHostAddress)
                           && op.Contains(s.MsgCustom04.Trim()));
        var filteredStudents = result.MultipleFilter(filterCollection.Filters);
        int coun = filteredStudents.Count();
        var pagedData = filteredStudents.OrderBy(t => t.MsgDateTime).Skip((page-1) * pageSize).Take(pageSize);
        var pagedDataList = pagedData.ToList();
        var jsonData = new { total = coun, pagedDataList };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }

和我的MultipleFilter方法是:

public static IQueryable<T> MultipleFilter<T>(this IQueryable<T> data,
      List<KendoGridFilter> filterExpressions)
    {
        if ((filterExpressions == null) || (filterExpressions.Count <= 0))
        {
            return data;
        }
        IQueryable<T> filteredquery = from item in data select item;
        for (int i = 0; i < filterExpressions.Count; i++ )
        {
            var index = i;
            Func<T, bool> expression = item =>
                {
                    var filter = filterExpressions[index];
                    var itemValue = item.GetType()
                        .GetProperty(filter.Field)
                        .GetValue(item, null);
                    if (itemValue == null)
                    {
                        return false;
                    }
                    var value = filter.Value;
                    switch (filter.Operator)
                    {
                        case "eq":
                            return itemValue.ToString() == value;
                        case "startswith":
                            return itemValue.ToString().StartsWith(value);
                        case "contains":
                            return itemValue.ToString().Contains(value);
                        case "endswith":
                            return itemValue.ToString().EndsWith(value);
                    }
                    return true;
                };
            filteredquery = filteredquery.Where(expression).AsQueryable();
        }
        return filteredquery;
    }

一切都很好,但是当我在我的剑道网格中使用过滤器时,我在filteredStudents变量中出现了内存不足的异常。

服务器过滤剑道网格时内存不足异常

你可以在剑道UI论坛上找到一个例子。下面是该页面的一个示例,以便您可以了解该方法的工作原理:

 public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
    {
        using (var northwind = new NorthwindEntities())
        {
            IQueryable<Product> products = northwind.Products;
            //Convert the Product entities to ProductViewModel instances.
            DataSourceResult result = products.ToDataSourceResult(request, product => new ProductViewModel
                    {
                    ProductID = product.ProductID,
                    ProductName = product.ProductName,
                    UnitsInStock = product.UnitsInStock
                    });
            return Json(result);
        }
    }