我有以下存储库方法: -
public async Task<Skill> FindSkill(int id, params Expression<Func<Skill, object>>[] includeProperties)
{
var query = context.Skills.AsQueryable();
if (includeProperties != null )
query = includeProperties.Aggregate(query, (current, include) => current.Include(include));
return await query.SingleOrDefaultAsync(a => a.SkillID == id);
}
我将此方法称为: -
public async Task<ActionResult> Deactivate(int id, Byte[] timestamp = null)
{
var skill = await unitofwork.SkillRepository.FindSkill(id);
//snip
}
但是当我没有通过任何params Expression
时,我无法察觉,我也尝试了以下检查,但没有效果: -
includeProperties.Count() != 0 || includeProperties[0].Name == "0"
现在我已经注意到在VS内部,当传递空列表时会收到以下内容: -
答案 0 :(得分:2)
使用参数时,不提供任何参数。将创建一个空数组。你应该使用includeProperties.Length!= 0作为Nick Bailey在他的评论中提出的建议。