像这样将 Automapper 与通用存储库一起使用时:
/// <summary>
/// Gets all DTOs
/// </summary>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException"></exception>
public virtual IEnumerable<TDto> GetAll()
{
return Mapper.Map<List<TDto>>(_repository.ReadAll());
}
如果存储库未返回任何记录,Automapper 会抛出异常“序列不包含任何元素”。
完全有可能调用该方法时记录可能不存在。
有没有办法让 Automapper 在没有记录的情况下返回一个空的 IEnumerable 而不是抛出这个丑陋的异常?
最佳答案
引入一个从存储库读取的变量,检查存储库是否返回任何数据,如果它不为空则映射数据并返回它,否则返回空 IEnumerable ?
关于c# - Automapper:序列不包含任何元素。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45979626/