I have method that expects the following parameters:
public IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] includeProperties)
{
foreach (var includeProperty in includeProperties)
{
dbSet.Include(includeProperty);
}
return dbSet;
}
Here's how I pass in my params:
IQueryable<User> users = repo.GetAllIncluding(u => u.EmailNotices, u => u.EmailTemplatePlaceholders, u => u.Actions);
However, I need to be able to check certain conditions before I pass them in or not.
For example, if I have a variable useEmailNotices = false, then I don't want to pass in EmailNotices but if it's true then I do. I need to do this for all three. I know there's a long way to do this but I was hoping that there was a one line short way or a param builder function or something of that nature.
What about changing the signature of the method to
public IQueryable<TEntity> GetAllIncluding(IEnumerable<Expression<Func<TEntity, object>>> includeProperties)
defining your conditional logic elsewhere
var args = new List<Expression<Func<TEntity, object>>>();
if (useEmailNotices)
args.Add(u => u.EmailNotices);
and then simply calling the method like
IQueryable<User> users = repo.GetAllIncluding(args);
Instead of Params, use a List<T>, declared with the actual entity you wish to pass in:
var funcList = new List<Expression<Func<User, object>>>();
funcList.add(u => u.EmailTemplatePlaceholders);
if (useEmailNotices)
funcList.add(u => u.EmailNotices);
And the method signature would now be:
public IQueryable<TEntity> GetAllIncluding(List<Expression<Func<TEntity, object>> includeProperties)
{
foreach( ... )
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With