如何正确比较列表

时间:2012-04-03 21:07:50

标签: c# algorithm list optimization comparison

  

可能重复:
  Compare two Lists for differences

这是我的功能

    public List<String[]> comparableListsAreTheSame(List<String> baseList, List<String> resultList, int type)
    {
        if (type == 1) { }

        List<String> baseListCopy = baseList;
        List<String> resultListCopy = resultList;

        bool sameLength = (baseListCopy.Count == resultList.Count); // are 2 lists have the same length?

        List<String> Base = baseListCopy.Except(resultListCopy, StringComparer.InvariantCultureIgnoreCase).ToList(); //Keep unique values 
        List<String> Result = resultListCopy.Except(baseListCopy, StringComparer.InvariantCultureIgnoreCase).ToList(); //Keep unique values 

        List<String[]> blocksComparisonSet1 = new List<String[]>(); //we add blocks based on list1; so we could output them to excel
        List<String[]> blocksComparisonSet2 = new List<String[]>(); //we add blocks based on list2; so we could output them to excel
        List<String[]> blocksComparisonFinal = new List<String[]>(); //we combine list1 and list

        //-----------------------------------------------------------------

        if (Result.Count > 0 || Base.Count > 0)
        {
            foreach (String resultLine in Result) //loop over all lines in list 1
            {
                bool found = false; //if element in base i
                String[] resultLineArray = resultLine.Split('*'); //get array from the string
                foreach (String baseLine in Base)
                {
                    String[] baseLineArray = baseLine.Split('*');
                    if (resultLineArray[0].Equals(baseLineArray[0]) && resultLineArray[1].Equals(baseLineArray[1]) && resultLineArray[2].Equals(baseLineArray[2]) && resultLineArray[3].Equals(baseLineArray[3]))
                    {
                        String[] NA = new String[2]; //keep results
                        NA[0] = baseLine; //[0] for base
                        NA[1] = resultLine; //[1] for result
                        blocksComparisonSet1.Add(NA);
                        found = true;
                    }
                }

                if (!found)
                {
                    String[] NA = new String[2]; //keep results
                    NA[0] = "N/A"; //[0] for base
                    NA[1] = resultLine; //[1] for result
                    blocksComparisonSet1.Add(NA);
                }
            }


            //-----------------------------------------------------------------
            foreach (String baseLine in Base) //loop over all lines in list 2
            {
                bool found = false; //if element in base i
                String[] baseLineArray = baseLine.Split('*'); //get array from the string
                foreach (String resultLine in Result)
                {
                    String[] resultLineArray = resultLine.Split('*');
                    if (resultLineArray[0].Equals(baseLineArray[0]) && resultLineArray[1].Equals(baseLineArray[1]) && resultLineArray[2].Equals(baseLineArray[2]) && resultLineArray[3].Equals(baseLineArray[3]))
                    {
                        String[] NA = new String[2]; //keep results
                        NA[0] = baseLine; //[0] for base
                        NA[1] = resultLine; //[1] for result
                        blocksComparisonSet2.Add(NA);
                        found = true;
                    }
                }

                if (!found)
                {
                    String[] NA = new String[2]; //keep results
                    NA[0] = baseLine; //[0] for base
                    NA[1] = "N/A"; //[1] for result
                    blocksComparisonSet2.Add(NA);
                }
            }
        }

        //-----------------------------------------------------------------
        if (blocksComparisonSet1.Any() || blocksComparisonSet2.Any()) //check if we have any values in out differences lists. if we do, merge them
        {
            blocksComparisonFinal.AddRange(blocksComparisonSet1); //add records from one list to final list
            blocksComparisonFinal.AddRange(blocksComparisonSet2); //add records from second list to final list
            HashSet<String[]> s = new HashSet<String[]>(blocksComparisonFinal);
            blocksComparisonFinal = s.ToList();
        }
        blocksComparisonFinal = blocksComparisonSet1.Union(blocksComparisonSet2, new ArrayEqualityComparer<string>()).ToList();
        return blocksComparisonFinal;
    }

我是C#的新手并且编程一般而且我做了多个循环并以漂亮的barabric方式匹配所有内容。我可以采用更专业的方式来做到更清洁,更合适吗?

2 个答案:

答案 0 :(得分:1)

我只有几条评论。

您最内层的foreach循环可以用List.Contains方法替换。你可以通过将它分成一个数组来添加一堆开销,然后在你可以直接比较字符串时循环遍历那个数组。 http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx

此外,循环遍历列表2中所有行的第二个循环只需要跟踪未命中而不是命中。第一个循环找到(1&amp; 2)和(1&amp; not 2)中的项目,因此只有在有意义的情况下才需要找到第二个循环(不是1&amp; 2)。这也将使你不必在最后将命中/未命中列表合并在一起。

如果你倾向于先对列表进行排序,你可以更有效,更干净地完成这项工作。

我希望这可以提供帮助。

答案 1 :(得分:1)

如果您正在检查listA和listB是否具有相同的元素,则可以使用此扩展方法:

public static IEnumerable<TSource> Intersect<TSource>
(
    this IEnumerable<TSource> first,
    IEnumerable<TSource> second,
    Func<TSource, TSource, bool> comparer
)
{
    return first.Intersect(second, new LambdaComparer<TSource>(comparer));
}

使用LambdaComparer类。

然后您可以这样比较它们:

var compared = listA.Intersect(listB, (a, b) => a == b);
if(compared.Count() == listA.Count())
   // they are the same
else
   // they are not