dotnetnoob
dotnetnoob

Reputation: 11330

How to add a where clause to this Entity Framework Linq statement

I have set up EF 4.3 using a generic respository/uow pattern.

A typical method looks like this:

using (var uow = new UnitOfWork(ConnectionString.PaydayLenders))
{
    var r = new CrudRepo<Tier>(uow.Context);
    return r.Find()
    .Include("CommissionTiers.MatchService.Provider")
    .ToList();
}

As you can see, I also include other tables in the fetch using navigation properties.

In this example, a Tier has many CommissionTiers, CommissionTiers has a MatchService and MatchService has a provider.

What I want to be able to do is select where CommissionTier.Status = 1 and Provider = 1. Is there a way this can be incorporated into this statement/method?

Upvotes: 1

Views: 2125

Answers (2)

Kamran Ajmal
Kamran Ajmal

Reputation: 302

You can use Join to get your required functionality.

Upvotes: -1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

Try this:

Where(t => t.CommissionTiers
            .Any(ct => ct.Status == 1 && ct.MatchService.Provider == 1))

Because you have navigation properties in place there is no need for an explicit join in your LINQ statement.

Upvotes: 2

Related Questions