Question

I have a GenericRepository with this function:

public virtual IEnumerable<TEntity> Get(
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
    int? Page=0,
    params Expression<Func<TEntity, bool>>[] filter)
{
    IQueryable<TEntity> query = dbSet;

    if (filter != null)
    {
        foreach (var z in filter)
        {
            query = query.Where(z);
        }

    }
    if (orderBy != null)
    {
        return orderBy(query).ToList();
    }
    else
    {
        return query.ToList();
    }
}

In one of my controllers I need also use include clauses so I decided to create new class DeviceInstanceRepository which will inherit GenericRepository and create new GetFiltered method and i got something like this:

public IEnumerable<DeviceInstance> GetFiltered(
    Func<IQueryable<DeviceInstance>, IOrderedQueryable<DeviceInstance>> orderBy = null, 
    int? Page = 0, 
    **WhatTypeShouldIUSeHere?** System.Linq.Expressions.Expression<Func<DeviceInstance,bool>>[] include ,
    params System.Linq.Expressions.Expression<Func<DeviceInstance, bool>>[] filter)
{
    IQueryable<DeviceInstance> query = dbSet;

    if(include!=null)
    {
        foreach(var z in include)
        {
            query = query.Include(z);
        }
    }
    if (filter != null)
    {
        foreach (var z in filter)
        {
            query = query.Where(z);
        }
    }
    if (orderBy != null)
    {
        return orderBy(query).ToList();
    }
    else
    {
        return query.ToList();
    }
}

But I have problems with parameters type. I know I can't pass two params array. Can you suggest me how to modify this line:

params System.Linq.Expressions.Expression<Func<DeviceInstance,bool>>[] include ,

to make things working?

Was it helpful?

Solution

I'm not sure what you are trying to achieve using such approach, but if you strongly want to pass two params parameters in your method, you can merge them into one params, just need also pass number of one of them. Like this (notice Take and Skip there):

    public IEnumerable<DeviceInstance> GetFiltered(
        Func<IQueryable<DeviceInstance>, IOrderedQueryable<DeviceInstance>> orderBy = null, 
        int? Page = 0,
        int numberOfIncludeExpressions = 0,
        params System.Linq.Expressions.Expression<Func<DeviceInstance, bool>>[] expressions)
    {
        IQueryable<DeviceInstance> query = dbSet;

        if (expressions != null)
        {
            foreach (var z in expressions.Take(numberOfIncludeExpressions))
            {
                query = query.Include(z);
            }

            foreach (var z in expressions.Skip(numberOfIncludeExpressions))
            {
                query = query.Where(z);
            }
        }
        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top