haansi
haansi

Reputation: 5738

how to add multiple conditions in this lambda function?

I am working with EF generic repository and have this function.

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

It is working fine and before I have used this function in a class like this:

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

Because I just have started using LINQ I don't have a good hand on it. Can you please guide me how I should add and (& with condition) in this condition. I want to modify it and add another condition that Name columns should not be empty.

Please note this instance is Period so repository is PeriodRepository.

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

Upvotes: 0

Views: 10298

Answers (2)

Tamir Dresher
Tamir Dresher

Reputation: 194

this will also work

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

Upvotes: 1

Gabe
Gabe

Reputation: 50475

This should work:

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

Upvotes: 7

Related Questions