【问题标题】:how to add multiple conditions in this lambda function?如何在这个 lambda 函数中添加多个条件?
【发布时间】:2012-08-10 00:36:19
【问题描述】:

我正在使用 EF 通用存储库并具有此功能。

public IEnumerable<T> Query(Expression<Func<T, bool>> filter)
    {
        return objectSet.Where(filter);
    }

它工作正常,在我在这样的类中使用此功能之前:

 private void BindProbabationPeriod()
    {
        ddlProbabationPeriod.DataSource = context.PeriodRepository.Query(a => a.EntityId == selectedEntityId);
        ddlProbabationPeriod.ValueMember = "Id";
        ddlProbabationPeriod.DisplayMember = "ProbabationPeriod";
    }

因为我刚刚开始使用 LINQ,所以我没有掌握它。你能指导我如何在这种情况下添加和(和条件)。我想修改它并添加另一个条件,即名称列不应为空。

请注意这个实例是 Period 所以存储库是 PeriodRepository。

context.PeriodRepository.Query(a => a.EntityId == selectedEntityId and a.Name!=null);

【问题讨论】:

    标签: c# .net linq entity-framework c#-4.0


    【解决方案1】:

    这应该可行:

    context.PeriodRepository.Query(a => a.EntityId == selectedEntityId && a.Name != null);
    

    【讨论】:

      【解决方案2】:

      这也行

      context.PeriodRepository.Query(a => a.EntityId == selectedEntityId).Where(a =>  a.Name != null);
      

      【讨论】:

        猜你喜欢
        • 2019-04-04
        • 1970-01-01
        • 2020-09-12
        • 2019-10-17
        • 2021-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多