ReadOnlyCollection在循环中重复

时间:2019-03-29 13:47:50

标签: c# collections foreach

我有一个方法,该方法从外部资源获取数据,将其添加到ReadOnlyCollection并返回此集合:

    public ReadOnlyCollection<TransactionDto> GetTransactions(int carrierId, DateTime beginDate, DateTime? endDate = null)
    {
        // call an external resource and save result to resultArray
        return _mapper.Map<ReadOnlyCollection<TransactionDto>>(resultArray);
    }

我的另一种方法:

    public List<string> Run(DateTime startDate, DateTime endDate)
    {
        List<string> transactionsJson = new List<string>();
        foreach (var carrierId in carrierIds)
        {
            var transactions = efsService.GetTransactions(
                carrierId, startDate, endDate);

            if (transactions.Count > 0)
            {
                var transactionJson = JsonConvert.SerializeObject(transactions);
                transactionsJson.Add(transactionJson);
            }
        }
        return transactionsJson;
    }

问题是,如果方法GetTransactions返回某些carrierId的记录,则此ReadOnlyCollection类型的变量将为循环中的其余carrierId返回。就像缓存的数据一样。为什么会这样?

已添加

映射器在我的班级的ctor中声明:

        var config = new MapperConfiguration(cfg => cfg.AddProfile<AutoMapperTransactionProfile>());
        _mapper = config.CreateMapper();

AutoMapperTransactionProfile声明:

internal class AutoMapperTransactionProfile : AutoMapper.Profile
{
    public AutoMapperTransactionProfile()
    {
        CreateMap<WSTransaction, TransactionDto>();
        CreateMap<WSTransactionInfo, TransactionInfoDto>();
        CreateMap<WSTransactionLineItem, TransactionLineItemDto>();
    }
}

循环中仅使用_mapper的一个实例

1 个答案:

答案 0 :(得分:0)

从方法中删除循环,并调用外部资源仅获取一次交易详细信息。就像下面...

public List<string> Run(DateTime startDate, DateTime endDate, int carrierId)
    {
        List<string> transactionsJson = new List<string>();

            var transactions = efsService.GetTransactions(
                carrierId, startDate, endDate);

            if (transactions.Count > 0)
            {
                var transactionJson = JsonConvert.SerializeObject(transactions);
                transactionsJson.Add(transactionJson);
            }

        return transactionsJson;
    }

更新

我在没有映射器的情况下模拟了您的代码,它没有返回以前的carrierId的记录。仍不清楚如何声明类以及如何映射类。对我来说,这似乎是您的映射器或服务从以前的carrierId返回记录的问题。

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReadOnlyCollection
{
    class Program
    {

        static List<Tuple<int, string>> transactions = new List<Tuple<int, string>>();

        static void Main(string[] args)
        {
            transactions.Add(Tuple.Create(1, "1"));
            transactions.Add(Tuple.Create(1, "11"));
            transactions.Add(Tuple.Create(2, "2"));
            transactions.Add(Tuple.Create(2, "12"));
            transactions.Add(Tuple.Create(3, "3"));
            transactions.Add(Tuple.Create(3, "13"));
            transactions.Add(Tuple.Create(4, "4"));
            transactions.Add(Tuple.Create(5, "5"));
            transactions.Add(Tuple.Create(6, "6"));

            Run(DateTime.Now, DateTime.Now);
        }

        public static List<string> Run(DateTime startDate, DateTime endDate)
        {
            var carrierIds = new List<int>() { 1,2,3,4,5,6};

            List<string> transactionsJson = new List<string>();
            foreach (var carrierId in carrierIds)
            {
                var transactions = GetTransactions(
                    carrierId, startDate, endDate);

                if (transactions.Count > 0)
                {
                    var transactionJson = JsonConvert.SerializeObject(transactions);
                    transactionsJson.Add(transactionJson);
                }
            }
            return transactionsJson;
        }

        public static ReadOnlyCollection<string> GetTransactions(int carrierId, DateTime beginDate, DateTime? endDate = null)
        {
            // call an external resource and save result to resultArray
            return new ReadOnlyCollection<string>(transactions.Where(item => item.Item1 == carrierId).Select(item => item.Item2).ToList());
        }
    }
}