数据无法获取。linq+;谓语
我写了一个扩展名。这个扩展的过滤机制是翻转谓词对象。但是,我有个问题数据无法获取。linq+;谓语,linq,expression,where,predicate,Linq,Expression,Where,Predicate,我写了一个扩展名。这个扩展的过滤机制是翻转谓词对象。但是,我有个问题 public static IQueryable<T> Filter<T>(this IQueryable<T> data, FilterGroupExpression filter) { var source = data; Expression<Func<T, bool>> predicate;
public static IQueryable<T> Filter<T>(this IQueryable<T> data, FilterGroupExpression filter)
{
var source = data;
Expression<Func<T, bool>> predicate;
if (filter.Operator == FilterGroupOperators.And)
predicate = PredicateBuilder.True<T>();
else
predicate = PredicateBuilder.False<T>();
foreach (FilterRuleDescriptive ruleExp in filter.Rules)
{
if (filter.Operator == FilterGroupOperators.And)
predicate = predicate.And<T>(MakeExpression<T>(ruleExp));
else
predicate = predicate.Or<T>(MakeExpression<T>(ruleExp));
}
foreach (FilterGroupExpression groupExp in filter.Groups)
{
if (filter.Operator == FilterGroupOperators.And)
predicate = predicate.And<T>(SubGroupFilter<T>(groupExp));
else
predicate = predicate.Or<T>(SubGroupFilter<T>(groupExp));
}
return source.Where(predicate);
}
public static Expression<Func<T, bool>> SubGroupFilter<T>(FilterGroupExpression filter)
{
Expression<Func<T, bool>> predicate;
if (filter.Operator == FilterGroupOperators.And)
predicate = PredicateBuilder.True<T>();
else
predicate = PredicateBuilder.False<T>();
foreach (FilterRuleDescriptive ruleExp in filter.Rules)
{
if (filter.Operator == FilterGroupOperators.And)
predicate = predicate.And<T>(MakeExpression<T>(ruleExp));
else
predicate = predicate.Or<T>(MakeExpression<T>(ruleExp));
}
foreach (FilterGroupExpression groupExp in filter.Groups)
{
predicate = SubGroupFilter<T>(groupExp);
}
return predicate;
}
public static Expression<Func<T, bool>> MakeExpression<T>(FilterRuleDescriptive condition)
{
Expression currentExpr = null;
var param = Expression.Parameter(typeof(T), "param");
var prop = Expression.Property(param, condition.Field);
var propType = Nullable.GetUnderlyingType(prop.Type) ?? prop.Type;
ConstantExpression constant = null;
condition.Value = Convert.ChangeType(condition.Value, propType);
if (IsNullableType(prop.Type))
constant = Expression.Constant(condition.Value.ConvertToNullable<int>(), prop.Type);
else
constant = Expression.Constant(condition.Value, propType);
if (condition.Value.GetType() == typeof(string))
condition.Value = condition.Value.ToString().Replace("'", string.Empty);
switch (condition.Operator)
{
case FilterRuleOperator.Equals:
currentExpr = Expression.Equal(prop, constant);
break;
case FilterRuleOperator.DoesNotEqual:
currentExpr = Expression.NotEqual(prop, constant);
break;
case FilterRuleOperator.StartsWith:
currentExpr = Expression.Call(prop, typeof(string).GetMethod("StartsWith", new[] { typeof(string) }), constant);
break;
case FilterRuleOperator.EndsWith:
currentExpr = Expression.Call(prop, typeof(string).GetMethod("EndsWith", new[] { typeof(string) }), constant);
break;
case FilterRuleOperator.Contains:
currentExpr = Expression.Call(prop, typeof(string).GetMethod("Contains", new[] { typeof(string) }), constant);
break;
case FilterRuleOperator.IsGreaterThan:
currentExpr = Expression.GreaterThan(prop, constant);
break;
case FilterRuleOperator.IsGreaterThanOrEqual:
currentExpr = Expression.GreaterThanOrEqual(prop, constant);
break;
case FilterRuleOperator.IsLessThan:
currentExpr = Expression.LessThan(prop, constant);
break;
case FilterRuleOperator.IsLessThanOrEqual:
currentExpr = Expression.LessThanOrEqual(prop, constant);
break;
case FilterRuleOperator.IsNull:
currentExpr = Expression.Equal(prop, constant);
break;
case FilterRuleOperator.IsNotNull:
currentExpr = Expression.NotEqual(prop, constant);
break;
default:
throw new ArgumentOutOfRangeException();
}
return Expression.Lambda<Func<T, bool>>(currentExpr, param);
}
当LinQ询问时,0转换数据
result.Count()=0
有什么问题吗?
请帮帮我…:(检查您的程序是否使用了正确的数据库,然后使用Sql Profiler查看正在执行的Sql。我从未使用过Sql Profiler。我从sqlprofiler中观察到了问题。非常感谢您的帮助…:)
SELECT [t0].[Id], [t0].[Name], [t0].[SmallDescription], [t0].[Description], [t0].[Barcode], [t0].[CategoryId], [t0].[Code], [t0].[PurchasePrice], [t0].[Price], [t0].[OldPrice], [t0].[VatRatio], [t0].[MetaDescription], [t0].[MetaKeywords], [t0].[VariantFirstTypeId], [t0].[VariantSecondTypeId], [t0].[MarkId], [t0].[Mark], [t0].[Stok], [t0].[ShowCase], [t0].[Firm], [t0].[IsVariant], [t0].[IsValid], [t0].[IsSale] FROM [dbo].[Product] AS [t0] WHERE ([t0].[Code] LIKE @p0) AND ([t0].[Name] LIKE @p1) AND ([t0].[Price] >= @p2) AND ([t0].[Price] < @p3) AND (([t0].[SmallDescription] LIKE @p4) OR ([t0].[SmallDescription] LIKE @p5))
var result = source.Where(predicate);