Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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# 在分组的集合中检测重复的ID集_C#_List_Linq_Duplicates_Microsoft Dynamics - Fatal编程技术网

C# 在分组的集合中检测重复的ID集

C# 在分组的集合中检测重复的ID集,c#,list,linq,duplicates,microsoft-dynamics,C#,List,Linq,Duplicates,Microsoft Dynamics,我有以下课程: public class ExistingProposal { public Guid ProposalIdPK { get; set; } public int StatusCode { get; set; } public List<ExistingProposalElement> GoldenRecordElements { get; } = new List<ExistingProposalElement>(); } pu

我有以下课程:

public class ExistingProposal
{
    public Guid ProposalIdPK { get; set; }
    public int StatusCode { get; set; }
    public List<ExistingProposalElement> GoldenRecordElements { get; } = new List<ExistingProposalElement>();
}

public class ExistingProposalElement
{
    public Guid ProposalElementIdPK { get; set; }
    public Guid ProposalIdFK { get; set; } // foreignkey referencing ExistingProposal.ProposalIdPK
}
现在需要使用元素创建一个新提案,例如ID为5、6和7的元素。我的支票现在需要检测 已经有一个带有这些ID的提案

新ProposalElement ID包含在

List<Guid> newElements
列出新元素
我如何才能检测到已经有一个提案包含与我的列表“新元素”和特定“状态代码”中包含的相同ProposalElement ID

我假设它将与Linq“All”方法或类似方法相关,但我真的很坚持它,因为我在Linq方面并不是很先进


任何帮助都将不胜感激。

以下是我为测试这一点而设置的虚拟类。我设置了您提供的两个示例,然后是一个新的建议,以检查它是否已经存在

var existingProposal1 = new ExistingProposal
{
    Id = 1,
    GoldenRecordElements = new List<ExistingProposalElement>
    {
        new ExistingProposalElement{ Id = 1 },
        new ExistingProposalElement{ Id = 2 },
        new ExistingProposalElement{ Id = 3 },
    }
};

var existingProposal2 = new ExistingProposal
{
    Id = 2,
    GoldenRecordElements = new List<ExistingProposalElement>
    {
        new ExistingProposalElement{ Id = 4 },
        new ExistingProposalElement{ Id = 5 },
        new ExistingProposalElement{ Id = 6 },
    }
};

List<ExistingProposal> existingProposals = new List<ExistingProposal>
{
    existingProposal1,
    existingProposal2
};

var newElements = new List<int> { 1, 2, 3 };
var exists = existingProposals.Any(x => x.GoldenRecordElements
     .Select(y => y.Id)
     .Intersect(newElements).Count() == x.GoldenRecordElements.Count());

下面是我为测试这一点而设置的虚拟类。我设置了您提供的两个示例,然后是一个新的建议,以检查它是否已经存在

var existingProposal1 = new ExistingProposal
{
    Id = 1,
    GoldenRecordElements = new List<ExistingProposalElement>
    {
        new ExistingProposalElement{ Id = 1 },
        new ExistingProposalElement{ Id = 2 },
        new ExistingProposalElement{ Id = 3 },
    }
};

var existingProposal2 = new ExistingProposal
{
    Id = 2,
    GoldenRecordElements = new List<ExistingProposalElement>
    {
        new ExistingProposalElement{ Id = 4 },
        new ExistingProposalElement{ Id = 5 },
        new ExistingProposalElement{ Id = 6 },
    }
};

List<ExistingProposal> existingProposals = new List<ExistingProposal>
{
    existingProposal1,
    existingProposal2
};

var newElements = new List<int> { 1, 2, 3 };
var exists = existingProposals.Any(x => x.GoldenRecordElements
     .Select(y => y.Id)
     .Intersect(newElements).Count() == x.GoldenRecordElements.Count());

我建议使用SelectMany,因为我对您的理解是正确的。。这比其他解决方案更容易

var existingItemsWithStatusCode = existingProposals.SelectMany(s => s.GoldenRecordElements,
                    (s, p) => new { s.ProposalIdPK, s.StatusCode, p.ProposalElementIdPK })
                .Where(w => newProposals.SelectMany(s => s.GoldenRecordElements.Select(t => t.ProposalElementIdPK))
                    .Contains(w.ProposalElementIdPK)).Select(s=>new
                {
                    s.ProposalElementIdPK,s.StatusCode
                }).ToList();

var newItems = existingProposals.SelectMany(s => s.GoldenRecordElements,
                    (s, p) => new {s.ProposalIdPK, s.StatusCode, p.ProposalElementIdPK})
                .Where(w => !newProposals.SelectMany(s => s.GoldenRecordElements.Select(t => t.ProposalElementIdPK))
                    .Contains(w.ProposalElementIdPK)).ToList();

我建议使用SelectMany,因为我对您的理解是正确的。。这比其他解决方案更容易

var existingItemsWithStatusCode = existingProposals.SelectMany(s => s.GoldenRecordElements,
                    (s, p) => new { s.ProposalIdPK, s.StatusCode, p.ProposalElementIdPK })
                .Where(w => newProposals.SelectMany(s => s.GoldenRecordElements.Select(t => t.ProposalElementIdPK))
                    .Contains(w.ProposalElementIdPK)).Select(s=>new
                {
                    s.ProposalElementIdPK,s.StatusCode
                }).ToList();

var newItems = existingProposals.SelectMany(s => s.GoldenRecordElements,
                    (s, p) => new {s.ProposalIdPK, s.StatusCode, p.ProposalElementIdPK})
                .Where(w => !newProposals.SelectMany(s => s.GoldenRecordElements.Select(t => t.ProposalElementIdPK))
                    .Contains(w.ProposalElementIdPK)).ToList();

您想要元素的精确匹配吗?例如,如果您有一个新的提案5、6和7,您需要匹配,或者5和6应该匹配,或者5、6和8应该匹配?你明白了。我需要精确匹配…所以如果现有和新提案包含完全相同的ID,我需要匹配。如果我收到ID 5,6,7,并且已经有一个元素ID为5,6,7的现有提案,我需要得到一个true。例如,您想要元素的精确匹配吗?例如,如果您有一个新的提案5、6和7,您需要匹配,或者5和6应该匹配,或者5、6和8应该匹配?你明白了。我需要精确匹配…所以如果现有和新提案包含完全相同的ID,我需要匹配。如果我收到ID 5,6,7,并且已经有一个元素ID为5,6,7的现有提案,我需要得到一个真实的,例如,非常感谢您的回复。但我仍然忽略了一个事实,即我需要将列表中的ID与现有提案进行比较。因此,如果列表包含1,2,3,并且任何已经存在的建议都包含完全相同的元素ID,那么我需要得到一个true,例如。主要目标是,如果现有的方案已经包含相同的元素,则避免创建方案。我的代码所做的不是仅检查int-Id而不是Guid,而是相同的主体吗?是的,一般来说,你是对的,但如上所述,我需要对照列表进行检查,因为如果存在,则不应创建方案已经有一个提案使用了完全相同的元素,我对它进行了更改,以便它可以对照ID列表进行检查?这对我有帮助吗?还是说我离目标太远了?这看起来好多了:)我会试着给你反馈:)非常感谢你的回复。但我仍然忽略了一个事实,即我需要将列表中的ID与现有提案进行比较。因此,如果列表包含1,2,3,并且任何已经存在的建议都包含完全相同的元素ID,那么我需要得到一个true,例如。主要目标是,如果现有的方案已经包含相同的元素,则避免创建方案。我的代码所做的不是仅检查int-Id而不是Guid,而是相同的主体吗?是的,一般来说,你是对的,但如上所述,我需要对照列表进行检查,因为如果存在,则不应创建方案已经有一个提案使用了完全相同的元素,我对它进行了更改,以便它可以对照ID列表进行检查?这对我有帮助吗?还是说我离底线太远了?这看起来好多了:)我会尽力给你反馈:)非常感谢。如上所述,我需要对包含所有新(传入)元素ID的列表进行检查。如果在现有提案中找到这些ID,则根本不需要创建新提案。这些建议实际上都是在现有提案中进行检查的。非常感谢。如上所述,我需要对包含所有新(传入)元素ID的列表进行检查。如果在现有提案中找到这些ID,则根本不需要创建新提案。这些建议实际上都只是在现有建议的范围内进行检查