Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法返回重复项。_C#_Linq - Fatal编程技术网

C# 方法返回重复项。

C# 方法返回重复项。,c#,linq,C#,Linq,我有一个返回策略的方法,它正在成功返回,只是它也返回重复的策略 public void GetAllEligibleUnredeemedPoliciesForEachActiveAgentCodeForTheAgent() { AgentPoliciesForEachAgentCode = new List<DtoApp2LeadPolicy>(); foreach (var agentCode in AllOfTheAgentCodesFor

我有一个返回策略的方法,它正在成功返回,只是它也返回重复的策略

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()  
我的问题是,这是消除重复的正确方法吗?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);
            }

        }
    }

您有重复项,因为您从未实际清理代理策略。ToList创建列表的新实例,然后清除该实例,并丢失对它的引用,因为您没有将其缓存在变量中

只要去掉这个标签,你就应该是金色的

if (AgentPolicies != null) AgentPolicies.Clear();
或者你可以这样做:

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

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

这条线对代理政策没有任何作用。ToList.Clear;AgentPolicys.ToList.Clear不是清算代理策略。我打赌这很可能是您复制问题的根源。我应该完全删除它吗?可能不会!至少不完全如此。如果要清除序列,则需要清除序列。鉴于我们看不到该序列的定义,也看不到它是如何填充和使用的,我们无法为您提供关于如何执行该操作以及它与您的问题的关系的良好指导。我们尝试了这两种方法,但无法编译什么类型的AgentPolicys?它是可枚举的吗?列表
  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);
            }

        }
    }