Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何有条件地将参数传递到func数组_C#_Expression_Method Parameters - Fatal编程技术网

C# 如何有条件地将参数传递到func数组

C# 如何有条件地将参数传递到func数组,c#,expression,method-parameters,C#,Expression,Method Parameters,我的方法需要以下参数: public IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] includeProperties) { foreach (var includeProperty in includeProperties) { dbSet.Include(includeProperty); } re

我的方法需要以下参数:

public IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] includeProperties)
{
    foreach (var includeProperty in includeProperties)
    {
        dbSet.Include(includeProperty);
    }
    return dbSet;
}
public IQueryable GetAllIncluding(参数表达式[]includeProperties)
{
foreach(includeProperty中的var includeProperty)
{
数据集包括(包括财产);
}
返回dbSet;
}
以下是我如何传递我的参数:

IQueryable<User> users = repo.GetAllIncluding(u => u.EmailNotices, u => u.EmailTemplatePlaceholders, u => u.Actions);
IQueryable users=repo.getalling(u=>u.EmailNotices,u=>u.emailTemplatePlaceholder,u=>u.Actions);
但是,我需要能够检查某些条件,然后才能将它们传递进来


例如,如果我有一个变量
usemailnotices=false
,那么我不想传入
EmailNotices
,但如果它是
true
,那么我会传入。我需要为这三个人做这件事。我知道要做到这一点还有很长的路要走,但我希望有一条单行的捷径,或者有一个param builder函数或类似的性质。

将方法的签名更改为

public IQueryable<TEntity> GetAllIncluding(IEnumerable<Expression<Func<TEntity, object>>> includeProperties)
public IQueryable GetAllIncluding(IEnumerable includeProperties)
在别处定义条件逻辑

var args = new List<Expression<Func<TEntity, object>>>();
if (useEmailNotices)
    args.Add(u => u.EmailNotices);
var args=new List();
如果(使用电子邮件通知)
args.Add(u=>u.EmailNotices);
然后简单地调用如下方法

IQueryable<User> users = repo.GetAllIncluding(args);
IQueryable users=repo.GetAllIncluding(args);

不要使用参数,而是使用一个
列表
,声明您希望传入的实际实体:

var funcList = new List<Expression<Func<User, object>>>();

funcList.add(u => u.EmailTemplatePlaceholders);

if (useEmailNotices)
    funcList.add(u => u.EmailNotices);
var funcList=新列表();
添加(u=>u.EmailTemplatePlaceholders);
如果(使用电子邮件通知)
funcList.add(u=>u.EmailNotices);
方法签名现在是:

public IQueryable<TEntity> GetAllIncluding(List<Expression<Func<TEntity, object>> includeProperties)
{
    foreach( ... )
}

public IQueryable GetAllIncluding(ListRepository…exposing IQueryable…叹气…@Phill你有什么建议?事实上,我从exposing IQueryable中得到的代码片段是一个泄漏的抽象,如果你要公开的话,你最好从一开始就去掉存储库,直接使用你的UoW。其次,你有一个可能性,这意味着你正在使用一个基本的re肯定的……好的,谢谢你的反馈。我会调查的。@DavidKhaykin这里没有太多的选项,是吗?:)是的,我认为只有列表必须有用户实体,而不是通用的tenty。听起来这是我唯一的选择。我希望不必总是有这一行
var args=new List()
每次我都使用它,但我只是做了一个重写,这样在没有条件参数的情况下我仍然可以使用简单的方法。@RichC:如果您愿意,可以将其混淆成一个方法,但是如果需要条件,您必须声明一个列表并使用多行填充它。@DavidHaykin不太可能。如果它在泛型类或方法中,则n它对用户有效
tenty