【问题标题】:Expression tree syntax表达式树语法
【发布时间】:2017-03-11 06:04:27
【问题描述】:

我正在重构我的旧代码并遇到了一些语法问题:

public IQueryable<T> Get_all(Expression<Func<T, bool>> predicate = null, bool enableDetectChanges = true, bool enableTracking = true) {

            ActiveDbContext.Configuration.AutoDetectChangesEnabled = enableDetectChanges;

            //return all if predicate is null
            Expression<Func<T, bool>> whereAll = x => true;
            predicate = predicate ?? whereAll;

            var result = enableTracking ? _dbSet.Where(predicate) : _dbSet.AsNoTracking().Where(predicate);

            return result;
        }

问:有没有可能像 predicate = predicate ?? Expression&lt;Func&lt;T, bool&gt;&gt; whereAll = x =&gt; true; 这样的语法而不是 2 行?

谢谢

【问题讨论】:

  • predicate ?? (x =&gt; true)
  • 我使用表达式树,我需要返回表达式树,而不是 lambda (linq to sql)
  • 哦;)对不起,它的工作!伟大的!谢谢:)

标签: c# asp.net entity-framework lambda expression


【解决方案1】:
predicate ?? (x => true)

我认为它错了(我在返回类型 Func 或表达式树之间徘徊),但它有效。感谢 PetSerAl

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多