Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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#_Diff - Fatal编程技术网

C# 创建集合的差异

C# 创建集合的差异,c#,diff,C#,Diff,我想在C#(LINQ)中创建两个集合的“合并差异”。假设对以下2个字符串集合和输入集合进行排序: a - a b - c e - d 预期产出应为: a - a b - null null - c null - d e - null 现在,我有以下实现: public static IEnumerable<KeyValuePair<T, T>> Diff<T>(IEnumerable<T> a, IEnumerable<T>

我想在C#(LINQ)中创建两个集合的“合并差异”。假设对以下2个字符串集合和输入集合进行排序:

a - a
b - c
e - d
预期产出应为:

a - a
b - null
null - c
null - d
e - null
现在,我有以下实现:

    public static IEnumerable<KeyValuePair<T, T>> Diff<T>(IEnumerable<T> a, IEnumerable<T> b, IEqualityComparer<T> comparer)
    {
        List<T> listA = a.ToList();
        List<T> listB = b.ToList();
        int indexA = 0;
        int indexB = 0;

        while (indexA < listA.Count || indexB < listB.Count)
        {
            if (indexA == listA.Count && indexB < listB.Count)
            {
                yield return new KeyValuePair<T, T>(default(T), listB[indexB++]);
            }
            else if (indexA < listA.Count && indexB == listB.Count)
            {
                yield return new KeyValuePair<T, T>(listA[indexA++], default(T));
            }
            else if (comparer.Equals(listA[indexA], listB[indexB]))
            {
                yield return new KeyValuePair<T, T>(listA[indexA++], listB[indexB++]);
            }
            else
            {
                yield return new KeyValuePair<T, T>(listA[indexA++], default(T));
            }
        }
    }
有人能帮我吗,如何修复它(有没有现成的nuget软件包?)

更新:

谢谢@Daniel的回答,如果集合中不包含重复的记录,它就会起作用

在这种情况下,过滤重复项或不保证重复项不是选项

我面临的下一个问题是:

假设我有两个集合(字符串列表):

实际结果如下所示:

Result Collection
  Apple  -  Apple
  null   -  Peach
  Apple  -  null
  Peach  -  null
我所期望的是:

Result Collection
  Apple  -  Apple
  Apple  -  null
  Peach  -  Peach

在这种情况下是否可以检测和耦合元素?

正如其他人在评论中指出的那样,您可能需要重新编写问题,以更清楚地说明所需的输出是什么。对“diff-merge”做一些假设,我猜您需要的不仅仅是相等比较器。相等比较器只告诉您事物是否相等。我的猜测是你想比较小于等于大于

如果必须为该方法提供自定义比较器,则这是一个合理的实现:

    public static IEnumerable<KeyValuePair<T, T>> Diff<T>(IEnumerable<T> a, IEnumerable<T> b, IEqualityComparer<T> comparer)
    {
        List<T> listA = a.ToList();
        List<T> listB = b.ToList();
        int indexA = 0;
        int indexB = 0;

        while (indexA < listA.Count || indexB < listB.Count)
        {
            if (indexA == listA.Count && indexB < listB.Count)
            {
                yield return new KeyValuePair<T, T>(default(T), listB[indexB++]);
            }
            else if (indexA < listA.Count && indexB == listB.Count)
            {
                yield return new KeyValuePair<T, T>(listA[indexA++], default(T));
            }
            else if (comparer.Equals(listA[indexA], listB[indexB]))
            {
                yield return new KeyValuePair<T, T>(listA[indexA++], listB[indexB++]);
            }
            else
            {
                yield return new KeyValuePair<T, T>(listA[indexA++], default(T));
            }
        }
    }
公共静态IEnumerable Diff(IEnumerable a、IEnumerable b、IComparer比较器)
{
List listA=a.ToList();
列表b=b.ToList();
int indexA=0;
int indexB=0;
while(indexA

或者您可以放弃Comparer参数,在T上添加类型约束以实现IComparable接口,这是我的首选。您的问题仍然不是所有内容都清楚,所以请彻底测试我的答案或更改问题。

这两个差异看起来似乎都有道理。。。你能提出一个问题来澄清你不喜欢的那个有什么问题吗?在编辑的同时,考虑到图书馆/ NuGue/其他异地资源的问题是非主题的,你能把问题重述清楚吗?我不明白他们在问什么。什么是a、b、c、d和e?当你说“a-a”之类的话时,那是什么意思?这是否意味着a应该等于a?如果你认为你没有足够的信息来回答这个问题,那么在问题澄清到你认为可以回答的程度之前,不要发布问题的答案。这个问题并没有说明比较项目是否平等。@Servy我有足够的信心知道OP想要做什么,这就是为什么我发布了我的答案。在我的回答中,我要改进的是预先说明我所做的假设。我记得听过一些播客(可能是约翰·斯基特?)在StackOverfo上回答问题的乐趣之一就是找出OP在缺少某些信息的情况下实际上想要做什么。因此,你在回答问题时花了所有的时间,说这个问题不清楚,无法回答,尽管你认为这个问题很清楚,不含糊,原因是什么?帮助某人找出他们真正想问的问题并不意味着只是在没有足够信息的情况下对问题进行猜测,只是希望你是对的,而是意味着帮助改进问题,使其成为可回答的问题,然后在澄清问题后才予以回答。你所说的“花费所有时间”是什么意思?这只是我答案中的第一句和最后一句。OP提供了预期的输出,从中我有足够的信心我知道他/她试图做什么,我试图通过指出布尔等式是不够的来帮助他。我可以找到很多SO页面,其中的答案是OP必须做一些不同于他试图解决问题或思考问题的方式的事情。我看答案看得越久,我就越觉得我应该删除第一句和最后一句。再说一遍,不要说一些澄清是没有帮助的。第一段和最后一段的大部分内容都是你在解释问题的问题,而不仅仅是第一句和最后一句。它几乎包含除代码之外的所有内容。说问题不清楚,所以你只是猜测他们想要什么,并希望你猜对,与说他们充分解释的问题最好通过他们最初尝试的不同方法解决,两者之间有很大区别。后者好,前者不好。
Result Collection
  Apple  -  Apple
  Apple  -  null
  Peach  -  Peach