LINQ内部的MapTo()返回相同数据的多次迭代 [英] MapTo() inside of LINQ returning multiple iterations of same data

查看:411
本文介绍了LINQ内部的MapTo()返回相同数据的多次迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将.NET Core与ASP.NET Boilerplate框架一起使用.在我得到修复的一些代码中,我们有一个GetAll()方法,该方法应该从MsSql数据库返回行的集合.原始代码:

I am using .NET Core with the ASP.NET Boilerplate framework. In some code that I was given to fix, we had a GetAll() method that was supposed to return a collection of rows from the MsSql database. The original code:

public IEnumerable<GuidelineCategoriesDto> GetAll(bool activeFilter = true)
{
    return _guidelineCategoriesRepo.GetAll().Select(x => x.MapTo(new GuidelineCategoriesDto())).ToList();
}

我在使用此方法时遇到的问题是,它将返回数据库中尚未被软删除的最新行.但是,它将返回同一行的次数等于该表中未删除的行的数量.所以从本质上讲,我会得到一个包含所有重复项的IEnumerable.为了解决这个问题,我将此方法更改为以下方法.

The problem I was running into with this method, is that it would return the latest row that had not been soft-deleted in the database. However, it would return that same row a number of times equal to the amount of non-deleted rows in that table. So essentially I would get back an IEnumerable that contained all duplicates. To fix this, I changed this method to the following.

public IEnumerable<GuidelineCategoriesDto> GetAll(bool activeFilter = true)
{
    // return _guidelineCategoriesRepo.GetAll().Select(x => ObjectMapper.Map<GuidelineCategoriesDto>(x)).ToList();
    return ObjectMapper.Map<IEnumerable<GuidelineCategoriesDto>>(_guidelineCategoriesRepo.GetAll());
}

此问题(包括注释行)解决了我所有的问题,并返回了正确的数据.我的问题是,为什么我提到的第一种方法以这种方式起作用.我不习惯使用ObjectMapper来熟悉MapTo方法,但是从我发现的方法上,我仍然无法确定为什么它会像以前那样工作.

This (including the commented line) fixed all my issues and returned the correct data. My question is why the first method I mentioned acted in the way that it did. I am not familiar with the MapTo method as I'm used to using ObjectMapper, but from what I found about it I still cannot determine why it behaved in the way that it did.

我的DTO具有正确的AutoMap数据注释,并且DbSetDbContext中,如果有任何重要的话.

My DTO has the correct AutoMap data annotation and the DbSet is in the DbContext, if any of that matters.

推荐答案

此...

_guidelineCategoriesRepo.GetAll().Select(x => x.MapTo(new GuidelineCategoriesDto())).ToList();

...有效:

var dto = new GuidelineCategoriesDto();
_guidelineCategoriesRepo.GetAll().Select(x => x.MapTo(dto)).ToList();

换句话说,您正在将每个实体映射到相同的DTO.

In other words, you are mapping every entity to the same DTO.

由于最后一个实体的映射最后一次,这意味着从每个实体映射的DTO似乎是从最后一个实体映射的DTO的副本.但实际上是一个DTO出现了多次.

Since the last entity is mapped last, this means that the DTO mapped from every entity appears to be a duplicate of the DTO mapped from the last entity. But is actually one DTO appearing multiple times.

这将起作用:

_guidelineCategoriesRepo.GetAll().Select(x => x.MapTo<GuidelineCategoriesDto>()).ToList();


避免MapTo;使用ObjectMapper

来自 https://aspnetboilerplate.com/页面/文档/对象到对象的映射#mapto-扩展方法:


Avoid MapTo; use ObjectMapper

From https://aspnetboilerplate.com/Pages/Documents/Object-To-Object-Mapping#mapto-extension-methods:

由于MapTo扩展方法是static,因此它们使用AutoMapper的静态实例(Mapper.Instance).这对于应用程序代码来说既简单又好用,但是由于静态配置和映射器在不同的测试之间共享,因此相互影响.

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.

这篇关于LINQ内部的MapTo()返回相同数据的多次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆