C#比较集合

时间:2011-10-28 15:51:39

标签: c# collections

我是C#的新手,需要一些比较集合的帮助。我有两个List<string> 集合及其内容如下:

收藏旧: { “AAA”, “BBB”, “CCC”}

收藏新品: { “BBB”, “CCC”, “DDD”}

我想得到如下的集合:

收藏决赛: {“AAA”,“删除”; “BBB”,“保持”; “CCC”,“保持”; “DDD”,“添加”}

我该怎么做?

5 个答案:

答案 0 :(得分:8)

old.Except(new)会为您提供要删除的项目

new.Except(old)会为您提供要添加的内容

old.Intersect(new)会为您提供物品

(假设您不介意使用System.Linq命名空间)

或者如果您愿意,可以单独考虑每个项目并检查每个列表中是否存在

答案 1 :(得分:1)

            var oldList = new List<String>() {"AAA", "BBB", "CCC"};
            var newList = new List<String>() {"BBB", "CCC", "DDD"};

            var diffDictionary = new Dictionary<string, string>();

            foreach (var oldEntry in oldList)
            {
                diffDictionary.Add(oldEntry, "Remove");
            }

            foreach (var newEntry in newList)
            {
                if (diffDictionary.ContainsKey(newEntry))
                {
                    diffDictionary[newEntry] = "Keep";
                }
                else
                {
                    diffDictionary.Add(newEntry, "Add");
                }
            }

            foreach (var dDico in diffDictionary)
            {
                Console.WriteLine(string.Concat("Key: ", dDico.Key, " Value: ", dDico.Value));
            }

答案 2 :(得分:0)

您可以使用字典来执行此操作...

最后,字典中的每个元素都会告诉您删除或添加了多少项。

它会用计数来表示这一点,而不是一个简单的3状态标志......这是因为你可能已经添加或删除了重复的项目......如果你在第二个集合中插入3 AAA怎么办?

        string[] col1 = new string[] { "AAA", "BBB", "CCC" };
        string[] col2 = new string[] { "BBB", "CCC", "DDD" };

        Dictionary<string, int> colDic = new Dictionary<string, int>();
        foreach (var item in col1)
        {
            int num;
            if (colDic.TryGetValue(item, out num))
                colDic[item] = num - 1;
            else
                colDic[item] = -1;
        }

        foreach (var item in col2)
        {
            int num;
            if (colDic.TryGetValue(item, out num))
                colDic[item] = num + 1;
            else
                colDic[item] = 1;
        }

最终结果如下:

AAA = -1
BBB = 0
CCC = 0
DDD = 1

答案 3 :(得分:0)

1行(有点)!

string[] colOld = {"AAA","BBB","CCC"};
string[] colNew = {"BBB","CCC","DDD"};

dynamic colALL = (from o in colOld.Union(colNew)
                  select new {Value = o, Action = 
                              colOld.Any(s => s == o) ? 
                                  colNew.Any(s => s == o) ? "Keep" : "Remove" 
                              : "Add"
                           }).ToList();

注意:这是以下vb.net的开发人员融合转换,它确实有效 - 我没有机会测试c#版本:

 Dim colOld() As String = {"AAA", "BBB", "CCC"}
    Dim colNew() As String = {"BBB", "CCC", "DDD"}

    Dim colALL = (From o As String In colOld.Union(colNew) _
                    Select New With {.Value = o, .Action = _
                                     If(colOld.Any(Function(s) s = o), _
                                        If(colNew.Any(Function(s) s = o), "Keep", "Remove"), _
                                        "Add")}).ToList

答案 4 :(得分:0)

如果你有这个方法

public static IEnumerable<T> Concat<T>(params IEnumerable<T>[] sequences)
{
    return sequences.SelectMany(x => x);
}

你应该写:

static readonly string Remove = "Remove";
static readonly string Keep = "Keep";
static readonly string Add = "Add";

var result = Concat
(
    old.Except(new).Select(x => new { x, Remove }), 
    old.Intersect(new).Select(x => new { x, Keep }), 
    new.Except(old).Select(x => new { x, Add })
);

当然你可以使用内置的Enumerable.Concat方法,但我发现它更优雅。