English 中文(简体)
C# Comparing Two Custom Lists
原标题:

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;
    }
最佳回答

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);
        }
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签