LINQ 内部的 MapTo() 返回相同数据的多次迭代
MapTo() inside of LINQ returning multiple iterations of same data
我正在使用 .NET Core 和 ASP.NET 样板框架。在我被要求修复的一些代码中,我们有一个 GetAll()
方法,该方法应该 return 来自 MsSql 数据库的行集合。原代码:
public IEnumerable<GuidelineCategoriesDto> GetAll(bool activeFilter = true)
{
return _guidelineCategoriesRepo.GetAll().Select(x => x.MapTo(new GuidelineCategoriesDto())).ToList();
}
我使用此方法 运行 遇到的问题是,它会 return 数据库中尚未软删除的最新行。但是,它将 return 同一行的次数等于 table 中未删除行的数量。所以基本上我会取回一个包含所有重复项的 IEnumerable
。为了解决这个问题,我将此方法更改为以下内容。
public IEnumerable<GuidelineCategoriesDto> GetAll(bool activeFilter = true)
{
// return _guidelineCategoriesRepo.GetAll().Select(x => ObjectMapper.Map<GuidelineCategoriesDto>(x)).ToList();
return ObjectMapper.Map<IEnumerable<GuidelineCategoriesDto>>(_guidelineCategoriesRepo.GetAll());
}
这(包括注释行)解决了我所有的问题并return编辑了正确的数据。我的问题是为什么我提到的第一种方法以它的方式起作用。我不熟悉 MapTo
方法,因为我习惯使用 ObjectMapper
,但从我发现的情况来看,我仍然无法确定为什么它会以这种方式运行。
我的 DTO 具有正确的 AutoMap
数据注释并且 DbSet
在 DbContext
中,如果有任何重要的话。
这个...
_guidelineCategoriesRepo.GetAll().Select(x => x.MapTo(new GuidelineCategoriesDto())).ToList();
...实际上是:
var dto = new GuidelineCategoriesDto();
_guidelineCategoriesRepo.GetAll().Select(x => x.MapTo(dto)).ToList();
换句话说,您正在将每个实体映射到同一个 DTO。
由于最后一个实体是最后映射的,这意味着从每个实体映射的 DTO 似乎是从最后一个实体映射的 DTO 的副本。但实际上是一个DTO多次出现
这可行:
_guidelineCategoriesRepo.GetAll().Select(x => x.MapTo<GuidelineCategoriesDto>()).ToList();
避免MapTo
;使用 ObjectMapper
来自https://aspnetboilerplate.com/Pages/Documents/Object-To-Object-Mapping#mapto-extension-methods:
Since the MapTo
extension methods are static
, they use AutoMapper's static instance (Mapper.Instance
). This is simple and fine for the application code, but you can have problems in unit tests since the static configuration and mapper is shared among different tests, all effecting each other.
我正在使用 .NET Core 和 ASP.NET 样板框架。在我被要求修复的一些代码中,我们有一个 GetAll()
方法,该方法应该 return 来自 MsSql 数据库的行集合。原代码:
public IEnumerable<GuidelineCategoriesDto> GetAll(bool activeFilter = true)
{
return _guidelineCategoriesRepo.GetAll().Select(x => x.MapTo(new GuidelineCategoriesDto())).ToList();
}
我使用此方法 运行 遇到的问题是,它会 return 数据库中尚未软删除的最新行。但是,它将 return 同一行的次数等于 table 中未删除行的数量。所以基本上我会取回一个包含所有重复项的 IEnumerable
。为了解决这个问题,我将此方法更改为以下内容。
public IEnumerable<GuidelineCategoriesDto> GetAll(bool activeFilter = true)
{
// return _guidelineCategoriesRepo.GetAll().Select(x => ObjectMapper.Map<GuidelineCategoriesDto>(x)).ToList();
return ObjectMapper.Map<IEnumerable<GuidelineCategoriesDto>>(_guidelineCategoriesRepo.GetAll());
}
这(包括注释行)解决了我所有的问题并return编辑了正确的数据。我的问题是为什么我提到的第一种方法以它的方式起作用。我不熟悉 MapTo
方法,因为我习惯使用 ObjectMapper
,但从我发现的情况来看,我仍然无法确定为什么它会以这种方式运行。
我的 DTO 具有正确的 AutoMap
数据注释并且 DbSet
在 DbContext
中,如果有任何重要的话。
这个...
_guidelineCategoriesRepo.GetAll().Select(x => x.MapTo(new GuidelineCategoriesDto())).ToList();
...实际上是:
var dto = new GuidelineCategoriesDto();
_guidelineCategoriesRepo.GetAll().Select(x => x.MapTo(dto)).ToList();
换句话说,您正在将每个实体映射到同一个 DTO。
由于最后一个实体是最后映射的,这意味着从每个实体映射的 DTO 似乎是从最后一个实体映射的 DTO 的副本。但实际上是一个DTO多次出现
这可行:
_guidelineCategoriesRepo.GetAll().Select(x => x.MapTo<GuidelineCategoriesDto>()).ToList();
避免MapTo
;使用 ObjectMapper
来自https://aspnetboilerplate.com/Pages/Documents/Object-To-Object-Mapping#mapto-extension-methods:
Since the
MapTo
extension methods arestatic
, they use AutoMapper's static instance (Mapper.Instance
). This is simple and fine for the application code, but you can have problems in unit tests since the static configuration and mapper is shared among different tests, all effecting each other.