.net mvc - 如何使 class 方法参数 (IQueryable) 可以为空
.net mvc - How to make class method parameter (IQueryable) to be nullable
我有一个接收可查询参数的 class 方法:
public IQueryable<Company> getEmpresasVisiblesUsuario(IQueryable<Company> companies, bool onlyActive = true)
{
/*
if (companies == null) {
companies = db.companies;
}
*/
int userId = HttpContext.Current.User.Identity.GetUserId<int>();
companies = companies.Where(s => s.id_user == userId);
if (onlyActive) {
companies = companies.Where(e => e.active);
}
return companies;
}
这工作正常,但现在我需要参数 'companies' 是可选的...我该怎么做?
我试过 IQueryable?公司,但这给出了一个错误:
the type IQueryable<Company> must be a non-nullable value type in order to use it as a parameter 'T' in the generic type or method 'Nullable<T>'
Em,给参数加上null就可以了:
public IQueryable<Company> getEmpresasVisiblesUsuario(IQueryable<Company> companies = null, bool onlyActive = true)
给公司参数一个默认值(即 null)
public IQueryable<Company> getEmpresasVisiblesUsuario(IQueryable<Company> companies = null, bool onlyActive = true)
那么你的方法可以调用如下
getEmpresasVisiblesUsuario(companies, false);
getEmpresasVisiblesUsuario(companies);
getEmpresasVisiblesUsuario(onlyActive: false);
您可以重载您的方法:
public IQueryable<Company> getEmpresasVisiblesUsuario(bool onlyActive = true)
{
return getEmpresasVisiblesUsuario(db.companies, onlyActive);
}
public IQueryable<Company> getEmpresasVisiblesUsuario(IQueryable<Company> companies, bool onlyActive = true)
{
int userId = HttpContext.Current.User.Identity.GetUserId<int>();
companies = companies.Where(s => s.id_user == userId);
if (onlyActive) {
companies = companies.Where(e => e.active);
}
return companies;
}
我有一个接收可查询参数的 class 方法:
public IQueryable<Company> getEmpresasVisiblesUsuario(IQueryable<Company> companies, bool onlyActive = true)
{
/*
if (companies == null) {
companies = db.companies;
}
*/
int userId = HttpContext.Current.User.Identity.GetUserId<int>();
companies = companies.Where(s => s.id_user == userId);
if (onlyActive) {
companies = companies.Where(e => e.active);
}
return companies;
}
这工作正常,但现在我需要参数 'companies' 是可选的...我该怎么做? 我试过 IQueryable?公司,但这给出了一个错误:
the type IQueryable<Company> must be a non-nullable value type in order to use it as a parameter 'T' in the generic type or method 'Nullable<T>'
Em,给参数加上null就可以了:
public IQueryable<Company> getEmpresasVisiblesUsuario(IQueryable<Company> companies = null, bool onlyActive = true)
给公司参数一个默认值(即 null)
public IQueryable<Company> getEmpresasVisiblesUsuario(IQueryable<Company> companies = null, bool onlyActive = true)
那么你的方法可以调用如下
getEmpresasVisiblesUsuario(companies, false);
getEmpresasVisiblesUsuario(companies);
getEmpresasVisiblesUsuario(onlyActive: false);
您可以重载您的方法:
public IQueryable<Company> getEmpresasVisiblesUsuario(bool onlyActive = true)
{
return getEmpresasVisiblesUsuario(db.companies, onlyActive);
}
public IQueryable<Company> getEmpresasVisiblesUsuario(IQueryable<Company> companies, bool onlyActive = true)
{
int userId = HttpContext.Current.User.Identity.GetUserId<int>();
companies = companies.Where(s => s.id_user == userId);
if (onlyActive) {
companies = companies.Where(e => e.active);
}
return companies;
}