IncludeFilter inGeneric Repository

c# entity-framework-6 entity-framework-plus linq

Question

I need filter in includes also. I have Record Status in all tables. So i need to filter the records based on this. To accommodate this, I am using Z.EntityFramework.Plus's IncludeFilter method.

My generic data code below;

    public async Task<T> GetAsync(int id, params Expression<Func<T, object>>[] includes)
    {
        var query = _context.Set<T>().AsQueryable(); //.FindAsync(id);

        if (includes.Length > 0)
        {
            query = includes.Aggregate(query,
              (current, include) => current.IncludeFilter(include));
        }
        var item = await query.FirstOrDefaultAsync(x => x.Id == id);
        return item;
    }

When i include where condition from the calling method (business class) its working fine. But i don't want to include these where condition in all places. So i need to change this method to include the where condition hereitself.

Please help me to do. I didn't get any where extension in my include parameter

Note: I need to include where condition in each of the include.

Popular Answer

In theory, you could make all of your entity types implement an interface (IRecord?) with the RecordStatus property on it.

Then you could change your includes parameter to Expression<Func<T, IRecord>>[].

The tricky part would be to construct new include expressions, based on the ones you're given, adding an && r.RecordStatus == "whatever" onto each of them. The code to do something like that can be a little ugly, but it should be possible.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why