方法返回重复项

本文关键字:返回 方法 | 更新日期: 2023-09-27 18:01:30

我有一个返回策略的方法,它成功地返回了,只是它也返回了重复项。

public void GetAllEligibleUnredeemedPoliciesForEachActiveAgentCodeForTheAgent()
    {
        AgentPoliciesForEachAgentCode = new List<DtoApp2LeadPolicy>();
        foreach (var agentCode in AllOfTheAgentCodesForTheAgent)
        {
            if (AgentPolicies != null) AgentPolicies.ToList().Clear();
            SetTheAgentCode(agentCode);
            SetAgentPolicyNumbersByAgentCode();
            SetAllPolicyNumbersByAgentsEligiblePolicies();
            SetAgentPoliciesFromAtlamServices();
            if(AgentPolicies != null) AgentPoliciesForEachAgentCode.AddRange(AgentPolicies);
        }
    }

所以我想添加

List<long> unique = longs.Distinct().ToList()  

我的问题是A(这是消除重复的正确方法吗?B( 在我的方法中,我应该在哪里以及如何插入?

我也试过这样做,说它包含一个无效的论点。

  public void GetAllEligibleUnredeemedPoliciesForEachActiveAgentCodeForTheAgent()
    {
        var AgentPoliciesForEachAgentCode = new List<DtoApp2LeadPolicy>();
        foreach (var agentCode in AllOfTheAgentCodesForTheAgent)
        {
            if (AgentPolicies != null) AgentPolicies.ToList().Clear();
            SetTheAgentCode(agentCode);
            SetAgentPolicyNumbersByAgentCode();
            SetAllPolicyNumbersByAgentsEligiblePolicies();
            SetAgentPoliciesFromAtlamServices();
            if(AgentPolicies != null && !AgentPoliciesForEachAgentCode.Contains(AgentPolicies))
            {
                AgentPoliciesForEachAgentCode.AddRange(AgentPolicies);
            }
        }
    }

方法返回重复项

  public void GetAllEligibleUnredeemedPoliciesForEachActiveAgentCodeForTheAgent()
    {
        var AgentPoliciesForEachAgentCode = new List<DtoApp2LeadPolicy>();
        foreach (var agentCode in AllOfTheAgentCodesForTheAgent.Distinct())
        {
            if (AgentPolicies != null) AgentPolicies.Clear();
            SetTheAgentCode(agentCode);
            SetAgentPolicyNumbersByAgentCode();
            SetAllPolicyNumbersByAgentsEligiblePolicies();
            SetAgentPoliciesFromAtlamServices();
            if (AgentPolicies != null )
            {
                AgentPoliciesForEachAgentCode.AddRange(AgentPolicies);
            }
        }
    }

因此,在这里,您只通过唯一(不同(的代理代码进行循环,而不是将重复项添加到AgentPolicyForEachAgentCode列表中。请尝试一下,让我知道

您有重复项,因为您从未真正清除AgentPoliciesToList()创建列表的一个新实例,然后清除该实例,并由于没有将其缓存在变量中而丢失对它的引用。

只要去掉ToList(),你就应该是金色的。

if (AgentPolicies != null) AgentPolicies.Clear();

或者你可以这样做:

if (AgentPolicies != null) AgentPolicies = new List<AgentPolicy>();