C#比较两个自定义列表

时间:2009-11-25 21:30:50

标签: c# compare custom-lists

我遇到过需要将两个不同的列表相互比较的情况,我想知道这样做的最佳方法是什么?我觉得这样的东西会起作用,但事实并非如此,我无法弄清楚原因。 Linq查询返回它不应该返回的记录。这是我第一次试图想出这样的东西,所以它无疑是凌乱的。

 private static List<ColumnDefinition> FindTableStructureUpdates(List<ColumnDefinition> colDefs, List<ColumnDefinition> tblCols)
    {
        List<ColumnDefinition> ColsToUpdate = new List<ColumnDefinition>();

        for (int i = 0; i < colDefs.Count; ++i)
        {
            string colDefName = colDefs[i].ColName;
            string colDefDataType = colDefs[i].ColType;
            string colDefAttribute = colDefs[i].ColAttributes;

            var query = from tbl in tblCols
                        where tbl.ColName != colDefName && tbl.ColType != colDefDataType && tbl.ColAttributes != colDefAttribute
                        select new { colDefName, colDefDataType, colDefAttribute };

            if (query.Count() > 0)
            {
                foreach (var item in query)
                {
                    ColsToUpdate.Add(new ColumnDefinition(item.colDefName, item.colDefDataType, item.colDefAttribute));
                }
            }
        }

        return ColsToUpdate;

任何建议都会很棒。

感谢。

IEquatable Implementation ??

 #region IEquatable<ColumnDefinition> Members

    public bool Equals(ColumnDefinition other)
    {
        if (this.ColName.Equals(other.ColName) && this.ColType.Equals(other.ColType) && this.ColAttributes.Equals(other.ColAttributes))
            return true;

        return false;
    }

1 个答案:

答案 0 :(得分:2)

你不能使用Enumerable.Except吗?

public static IEnumerable<TSource> Except<TSource>(
    this IEnumerable<TSource> first,
    IEnumerable<TSource> second
)

更多details

Snippet Compiler

中测试的示例
using System;
using System.Linq;
using System.Collections.Generic;

class ColumnDefinition : IEquatable<ColumnDefinition>
{
    public string Name { get; set; }
    public string Type { get; set; }
    public string Attr { get; set; }

    public ColumnDefinition()
    {
        Name = string.Empty;
        Type = string.Empty;
        Attr = string.Empty;
    }

    public bool Equals(ColumnDefinition other)
    {   
        return Name.Equals(other.Name) && Type.Equals(other.Type) && Attr.Equals(other.Attr);
    }

    public override bool Equals(object value)
    {
        return (value is ColumnDefinition) ? Equals(value as ColumnDefinition) : false;
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode() ^ Type.GetHashCode() ^ Attr.GetHashCode();
    }

    public override string ToString()
    {
        return string.Concat("{", Name, ":", Type, ":", Attr, "}");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            MyMain(args);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        finally
        {
            Console.ReadKey();
        }
    }

    public static void MyMain(string[] args)
    {
        var list1 = new []  
            { 
                new ColumnDefinition { Name = "foo", Type = "int", Attr = "0" }, 
                new ColumnDefinition { Name = "bar", Type = "int", Attr = "1" }, 
            };

        var list2 = new [] 
            { 
                new ColumnDefinition { Name = "foo", Type = "int", Attr = "0" }, 
                new ColumnDefinition { Name = "bar", Type = "string", Attr = "1" }, 
            };

        foreach (var changed in Enumerable.Except(list1, list2))
        {
            Console.WriteLine(changed);
        }
    }
}