开发者

Where clause to related entity

开发者 https://www.devze.com 2023-03-30 14:00 出处:网络
I have a generic repository pattern and I\'m trying to load a collection of Agencies based the FkApplicationId and if it IsEnabled == true

I have a generic repository pattern and I'm trying to load a collection of Agencies based the FkApplicationId and if it IsEnabled == true

My model looks something like this:

Where clause to related entity

I thought this would be easy but I can't create a where clause to filter the results. I can't see the properties of the AppAgencies to write a condition, this is as far as I can get:

public IEnumerable<Agency> GetAllEnabledAgencies(int applicationId)
{
    return _agencyRepository.GetMany(m => m.AppAgencies.//No Entity Properties are here);
}

From my repository base which is called from above:

public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
{
    return _dbSet.Where(where).ToList();
}

Thanks RPM1984, Solution:

Agencies are consumed by multiple applications, and they need the ability to enable/disable each one per application. So I was using the AppAgency table to tie together that req. because I don't want to have to add a new column to the Agency entity every time a new application is introduced.

public IEnumerable<Agency> GetAllEnabledAgencies(int applicationId)
{
     return _agencyRepository.GetMany(x => x.AppAg开发者_StackOverflowencies.Any(y => y.IsEnabled && y.FkApplicationId == applicationId));
}


There are no entity properties because the AppAgencies property on Agency is a navigational property.

I think this is what you want:

public IEnumerable<Agency> GetAllEnabledAgencies(int applicationId)
{
    return _agencyRepository.GetMany(x => x.AppAgencies.Any(y => y.IsEnabled));
}

In English:

Get all Agencies, where at least one AppAgency is enabled.

If you want:

Get all Agencies, where all AppAgency is enabled.

Change Any to All.

0

精彩评论

暂无评论...
验证码 换一张
取 消