0

我遇到了一种情况,我需要将两个不同的列表相互比较,我想知道最好的方法是什么?我认为这样的事情会起作用,但它不起作用,我不知道为什么。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 实现?

 #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;
    }
4

1 回答 1

2

你不能用Enumerable.Except吗?

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

更多细节

在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);
        }
    }
}
于 2009-11-25T21:41:40.733 回答