比较两个 List> 的正确方法在 C# 2.0 中

发布于 2024-09-28 20:20:53 字数 2418 浏览 3 评论 0 原文

这是 C# 2.0 中比较的正确方法吗(无 LINQ )。

下面的代码工作正常,但我认为这不是比较的好方法。

        List<KeyValuePair<string, foo>> list1 = new List<KeyValuePair<string, foo>>();
        List<KeyValuePair<string, foo>> list2 = new List<KeyValuePair<string, foo>>();
        List<foo> diffList = new List<foo>();

        list1.Add(new KeyValuePair<string, foo>("1", new foo("1", new Cost(1.0)));
        list1.Add(new KeyValuePair<string, foo>("2", new foo("2", new Cost(2.0)));
        list1.Add(new KeyValuePair<string, foo>("3", new foo("3", new Cost(3.0)));
        list1.Add(new KeyValuePair<string, foo>("5", new foo("5", new Cost(5.0)));

        list2.Add(new KeyValuePair<string, foo>("1", new foo("1", new Cost(1.0));
        list2.Add(new KeyValuePair<string, foo>("2", new foo("2", new Cost(2.1)));
        list2.Add(new KeyValuePair<string, foo>("4", new foo("4", new Cost(4.0));
        list2.Add(new KeyValuePair<string, foo>("6", new foo("6", new Cost(6.0)));
        list2.Add(new KeyValuePair<string, foo>("7", new foo("7", new Cost(7.0)));

        foreach (KeyValuePair<string, foo> pair1 in list1)
        {
            bool b = true;
            foreach (KeyValuePair<string, foo> pair2 in list2)
            {
                if (pair2.Key == pair1.Key)
                {
                    if (pair2.Value.Equals(pair1.Value))
                    {
                        list2.Remove(pair2);
                        break;
                    }
                    else
                    {
                        diffList.Add(pair2.Value);
                        diffList.Add(pair1.Value);
                        list2.Remove(pair2);
                        b = false;
                        break;
                    }
                }
                else
                {

                    diffList.Add(pair2.Value);
                    diffList.Add(pair1.Value);
                    list2.Remove(pair2);
                    b = false;
                    break;
                }
            }
            if (list2.Count == 0 && b)
            {
                diffList.Add(pair1.Value);
            }
        }
        foreach (KeyValuePair<string, foo> pair2 in list2)
        {
            diffList.Add(pair2.Value);
        }

Is this correct way to compare in C# 2.0 (NO LINQ ).

Below code is working ok but I think its not good way to compare.

        List<KeyValuePair<string, foo>> list1 = new List<KeyValuePair<string, foo>>();
        List<KeyValuePair<string, foo>> list2 = new List<KeyValuePair<string, foo>>();
        List<foo> diffList = new List<foo>();

        list1.Add(new KeyValuePair<string, foo>("1", new foo("1", new Cost(1.0)));
        list1.Add(new KeyValuePair<string, foo>("2", new foo("2", new Cost(2.0)));
        list1.Add(new KeyValuePair<string, foo>("3", new foo("3", new Cost(3.0)));
        list1.Add(new KeyValuePair<string, foo>("5", new foo("5", new Cost(5.0)));

        list2.Add(new KeyValuePair<string, foo>("1", new foo("1", new Cost(1.0));
        list2.Add(new KeyValuePair<string, foo>("2", new foo("2", new Cost(2.1)));
        list2.Add(new KeyValuePair<string, foo>("4", new foo("4", new Cost(4.0));
        list2.Add(new KeyValuePair<string, foo>("6", new foo("6", new Cost(6.0)));
        list2.Add(new KeyValuePair<string, foo>("7", new foo("7", new Cost(7.0)));

        foreach (KeyValuePair<string, foo> pair1 in list1)
        {
            bool b = true;
            foreach (KeyValuePair<string, foo> pair2 in list2)
            {
                if (pair2.Key == pair1.Key)
                {
                    if (pair2.Value.Equals(pair1.Value))
                    {
                        list2.Remove(pair2);
                        break;
                    }
                    else
                    {
                        diffList.Add(pair2.Value);
                        diffList.Add(pair1.Value);
                        list2.Remove(pair2);
                        b = false;
                        break;
                    }
                }
                else
                {

                    diffList.Add(pair2.Value);
                    diffList.Add(pair1.Value);
                    list2.Remove(pair2);
                    b = false;
                    break;
                }
            }
            if (list2.Count == 0 && b)
            {
                diffList.Add(pair1.Value);
            }
        }
        foreach (KeyValuePair<string, foo> pair2 in list2)
        {
            diffList.Add(pair2.Value);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-10-05 20:20:53

这样做会更简单、更快:

  1. 将两个列表推入字典(或首先构建字典)。
  2. 迭代一个字典,查找另一字典中的每个键,相应地添加差异条目。仅添加您正在迭代的字典中的条目。
  3. 交换字典并重复循环。

It would be simpler and faster to:

  1. Push both lists into dictionaries (or build dictionaries in the first place).
  2. Iterate over one dictionary, looking up each key in the other dictionary, adding diff entries accordingly. Only add entries from the dictionary you are iterating over.
  3. Swap the dictionaries and repeat the loop.
献世佛 2024-10-05 20:20:53

马塞洛是对的,KeyValuePairs 列表总是更好地表示为字典,除非您由于某种原因希望能够拥有重复的键。

尝试这样的事情:

var a = list1.ToDictionary(i => i.Key, i => i.Value);
var b = list2.ToDictionary(i => i.Key, i => i.Value);
var result = new List<foo>();

foreach (var entry in a)
{
    if (b.ContainsKey(entry.Key) 
        && entry.Value != b[entry.Key])
    {
        result.Add(entry.Value);
        result.Add(b[entry.Key]);
    }
}
foreach (var entry in b)
{
    if (a.ContainsKey(entry.Key) 
        && entry.Value != a[entry.Key] 
        && !result.Contains(entry.Value))
    {
        result.Add(entry.Value);
        result.Add(a[entry.Key]);
    }
}

Marcelo is right, a list of KeyValuePairs is always better represented as a dictionary UNLESS you want to be able to have duplicate keys for some reason.

Try something like this:

var a = list1.ToDictionary(i => i.Key, i => i.Value);
var b = list2.ToDictionary(i => i.Key, i => i.Value);
var result = new List<foo>();

foreach (var entry in a)
{
    if (b.ContainsKey(entry.Key) 
        && entry.Value != b[entry.Key])
    {
        result.Add(entry.Value);
        result.Add(b[entry.Key]);
    }
}
foreach (var entry in b)
{
    if (a.ContainsKey(entry.Key) 
        && entry.Value != a[entry.Key] 
        && !result.Contains(entry.Value))
    {
        result.Add(entry.Value);
        result.Add(a[entry.Key]);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文