【问题标题】:How to perform LINQ where clause inside a method如何在方法中执行 LINQ where 子句
【发布时间】:2015-04-10 16:39:04
【问题描述】:

我想在方法中执行 LINQ where 子句。

例如:

using (BP_TTOKEntities db = new BP_TTOKEntities(_dto.IdTenant))
{
    var res = db.doc003fornitura

    if (fornitura.Numero != null) //Filtro numero
    {
        if (!fornitura.Numero.LBoundIsNull) res = res.Where(x => x.fornitura_nro >= fornitura.Numero.LBound);
        if (!fornitura.Numero.UBoundIsNull) res = res.Where(x => x.fornitura_nro <= fornitura.Numero.UBound);
    }
}

我会替换:

if (!fornitura.Numero.LBoundIsNull) res = res.Where(x => x.fornitura_nro >= fornitura.Numero.LBound);
if (!fornitura.Numero.UBoundIsNull) res = res.Where(x => x.fornitura_nro <= fornitura.Numero.UBound);

类似这样的:

res = fornitura.Numero.Where<doc003fornitura>(x.fornitura_nro);

有可能吗?如何制作方法?

谢谢路易吉。

【问题讨论】:

  • 写单行字往往不是解决生活问题的真正方法。对正确工作的有序代码感到满意。
  • 我们不知道任何所涉及的类型也无济于事。看起来fornitura.Numero 是一个单一的值,所以在它上面调用Where 是没有意义的。打电话给db.doc003fornitura.Where(...) 会更有意义。如果您能提供一个简短但完整的示例,最好使用更有意义(和传统)的名称,这将很有帮助。
  • 另外,您可能希望使用 if.. else if.. 模式而不是 if.. if.. 模式,因为 fornitura.Numero 将始终只有一个值,然后不会如果检查,则不会额外
  • bit ,你说的不对。第一个 if 执行 >= 检查,第二个

标签: c# linq entity-framework linq-to-entities


【解决方案1】:

我就是这样解决的。

这是电话:

if (fornitura.Numero != null) res = fornitura.Numero.Compare(res, x => x.fornitura_nro);

这些是类的方法:

            public IQueryable<TEntity> Compare<TEntity>(IQueryable<TEntity> source, Expression<Func<TEntity, Nullable<short>>> func)
            {
                return _Compare(source, func);
            }

            public IQueryable<TEntity> Compare<TEntity>(IQueryable<TEntity> source, Expression<Func<TEntity, Nullable<int>>> func)
            {
                return _Compare(source, func);
            }

            public IQueryable<TEntity> Compare<TEntity>(IQueryable<TEntity> source, Expression<Func<TEntity, Nullable<DateTime>>> func)
            {
                return _Compare(source, func);
            }

            public IQueryable<TEntity> Compare<TEntity>(IQueryable<TEntity> source, Expression<Func<TEntity, string>> func)
            {
                return _Compare(source, func);
            }

            private IQueryable<TEntity> _Compare<TEntity>(IQueryable<TEntity> source, object func)
            {
                IQueryable<TEntity> res = source;

                Type type = func.GetType();
                Expression funcBody = (Expression)type.GetProperty("Body").GetValue(func);
                IEnumerable<ParameterExpression> funcParameters = (IEnumerable<ParameterExpression>)type.GetProperty("Parameters").GetValue(func);

                if (!this.LBoundIsNull)
                {
                    Expression ge = _Comparison(funcBody, Expression.Constant(_lBound), ExpressionType.GreaterThanOrEqual);
                    var lambda = Expression.Lambda<Func<TEntity, bool>>(ge, funcParameters);
                    res = res.Where(lambda);
                }

                if (!this.UBoundIsNull)
                {
                    Expression le = _Comparison(funcBody, Expression.Constant(_uBound), ExpressionType.LessThanOrEqual);
                    var lambda = Expression.Lambda<Func<TEntity, bool>>(le, funcParameters);
                    res = res.Where(lambda);
                }

                return res;
            }

            private Expression _Comparison(Expression left, Expression right, ExpressionType expressionType)
            {
                if (left.Type.IsNullable() && !right.Type.IsNullable())
                    right = Expression.Convert(right, left.Type);
                else if (!left.Type.IsNullable() && right.Type.IsNullable())
                    left = Expression.Convert(left, right.Type);

                if (left.Type == typeof(string))
                {
                    var method = left.Type.GetMethod("CompareTo", new[] { typeof(string) });
                    var result = Expression.Call(left, method, right);
                    return Expression.MakeBinary(expressionType, result, Expression.Constant(0));
                }
                else
                    switch (expressionType)
                    {
                        case ExpressionType.GreaterThanOrEqual:
                            return Expression.GreaterThanOrEqual(left, right);
                        case ExpressionType.LessThanOrEqual:
                            return Expression.LessThanOrEqual(left, right);
                        default:
                            return Expression.Equal(left, right);
                    }
            }

谢谢!

【讨论】:

    猜你喜欢
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2020-10-27
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多