C# 比较两个自定义列表

发布于 2024-08-12 15:45:54 字数 1598 浏览 7 评论 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;
    }

I have run into a situation where I need to compare two different lists to each other and I am wondering what the best method is for doing this? I thought something like this would work but it doesn't and I can't figure out why. The Linq query is returning records it shouldn't. This is my first run at trying to figure something like this out so it is undoubtedly messy.

 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;

Any suggestions would be great.

Thanks.

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

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

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

发布评论

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

评论(1

沉溺在你眼里的海 2024-08-19 15:45:54

你不能使用Enumerable.Except吗?

public static IEnumerable;除了(
    此 IEnumerable第一的,
    IEnumerable第二
)

更多详细信息

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);
        }
    }
}

Can't you use Enumerable.Except ?

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

More details.

An example tested in 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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文