Chu*_*ris 23 c# function params
我有一个从数据库中获取值的方法.
 public virtual List<TEntity> GetValues(
           int? parameter1 = null,
           int? parameter2 = null,
           int? parameter3 = null,
           params Expression<Func<TEntity, object>>[] include)
        {
            //...
        } 
Run Code Online (Sandbox Code Playgroud)
如何使用命名参数调用此函数以不在之前写入所有参数include?我想做这样的事情
var userInfo1 = Unit.UserSrvc.GetValues(include: p => p.Membership, p => p.User);
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用?如何在params中使用命名参数?
svi*_*ick 12
我认为唯一的方法是:
GetValues(include:
   new Expression<Func<TEntity, object>>[] { p => p.Membership, p => p.User })
Run Code Online (Sandbox Code Playgroud)
哪个不是那么好.如果您为此添加了重载,那可能是最好的:
public List<Entity> GetValues(params Expression<Func<Entity, object>>[] include)
{
    return GetValues(null, null, null, include);
}
Run Code Online (Sandbox Code Playgroud)
然后你就像调用你的方法一样
GetValues(p => p.Membership, p => p.User)
Run Code Online (Sandbox Code Playgroud)
        一个params说法就像一个数组,试试这个语法:
var userInfo1 = Unit.UserSrvc.GetValues(include: new Expression<Func<TEntity, object>>[] { p => p.Membership, p => p.User });
Run Code Online (Sandbox Code Playgroud)
(由于通用参数,可能需要一些调整,但我认为你得到了它的要点)