/// <summary> /// Executes IQueryable list and returns DataResult includes data /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <param name="request"></param> /// <returns>DataResult</returns> public static DataResult <T> ToDataResult <T>(this IQueryable <T> query, DataRequest request) where T : class { var result = new DataResult <T> { draw = request.draw }; result.recordsTotal = result.recordsFiltered = query.Count(); foreach (var item in request.filters) { var exp = GetExpression <T>(item.Operand, item.Field, item.Value); if (exp != null) { query = query.Where(exp); } } var listExp = new List <FilterDef>(); if (!string.IsNullOrEmpty(request.search?.value)) { foreach (var item in request.columns.Where(a => a.searchable)) { ParameterExpression param = Expression.Parameter(typeof(T), "t"); MemberExpression member = Expression.Property(param, item.data); var operand = member.Type == typeof(string) ? Operand.Contains : Operand.Equal; listExp.Add(new FilterDef { Operand = operand, Field = item.data, Value = request.search.value, Operator = Operator.Or }); } } foreach (var item in request.columns.Where(a => a.searchable == true && !string.IsNullOrEmpty(a.search.value))) { ParameterExpression param = Expression.Parameter(typeof(T), "t"); MemberExpression member = Expression.Property(param, item.data); var operand = member.Type == typeof(string) ? Operand.Contains : Operand.Equal; listExp.Add(new FilterDef { Operand = operand, Field = item.data, Value = item.search.value, Operator = Operator.And }); } if (listExp.Any()) { Expression <Func <T, bool> > exp = null; exp = ExpressionBuilder.GetExpression <T>(listExp); if (exp != null) { query = query.Where(exp); } } if (listExp.Any() || request.filters.Any()) { result.recordsFiltered = query.Count(); } if (request.draw > 0) { if (!request.order.Any()) { query = query.OrderBy(request.columns[0].data); } else { foreach (var item in request.order) { if (item.dir == "asc") { query = query.OrderBy(request.columns[item.column].data); } else { query = query.OrderByDescending(request.columns[item.column].data); } } } query = query.Skip(request.start).Take(request.length); } result.data = query.ToList(); return(result); }
/// <summary> /// Executes IQueryable list and returns DataResult includes data /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <param name="request"></param> /// <returns>DataResult</returns> public static DataResult <T> ToDataResult <T>(this IQueryable <T> query, DataRequest request) where T : class { var result = new DataResult <T> { draw = request.draw }; result.recordsTotal = result.recordsFiltered = query.Count(); foreach (var item in request.filters) { var exp = GetExpression <T>((Operand)item.Operand, item.Field, item.Value); if (exp != null) { query = query.Where(exp); } } if (!string.IsNullOrEmpty(request.search?.value)) { Expression currentexp = null; ParameterExpression param = Expression.Parameter(typeof(T), "t"); foreach (var item in request.columns.Where(a => a.searchable)) { MemberExpression member = Expression.Property(param, item.data); var isString = member.Type == typeof(string); var operand = isString ? Operand.Contains : Operand.Equal; var words = request.search.value.Split(' ').Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); if (isString && words.Count > 1 && item.data.Contains("Name")) { Expression exp = null; foreach (var word in words) { var filter = new FilterDefinition { Operand = operand, Field = item.data, Value = word }; var expin = ExpressionBuilder.GetExpression <T>(param, filter); if (exp == null) { exp = expin; } else { exp = Expression.And(exp, expin); } } if (currentexp == null) { currentexp = exp; } else { currentexp = Expression.Or(currentexp, exp); } } else { var filter = new FilterDefinition { Operand = operand, Field = item.data, Value = request.search.value }; var expin = ExpressionBuilder.GetExpression <T>(param, filter); if (currentexp == null) { currentexp = expin; } else { currentexp = Expression.Or(currentexp, expin); } } } var exp2 = Expression.Lambda <Func <T, bool> >(currentexp, param); if (exp2 != null) { query = query.Where(exp2); } } if (!string.IsNullOrEmpty(request.search?.value) || request.filters.Any()) { result.recordsFiltered = query.Count(); } if (request.draw > 0) { if (!request.order.Any()) { query = query.OrderBy(request.columns[0].data); } else { foreach (var item in request.order) { if (item.dir == "asc") { query = query.OrderBy(request.columns[item.column].data); } else { query = query.OrderByDescending(request.columns[item.column].data); } } } query = query.Skip(request.start).Take(request.length); } result.data = query.ToList(); return(result); }