简体   繁体   中英

C# WPF compare List<T> to Datagrid.ItemsSource

I bound my private List<MaintenanceWindow> tempMaintenanceWindows to a Datagrid and enabled the user to edit the items in the Datagrid as well as to add new items. This works fine.

Now I thought about how to rollback the changes the user made if the window is closed without pressing the save button first. Basically, I want to compare the Datagrid.ItemsSource to the temporary List I populated like this:

foreach (MaintenanceWindow mainWin in maintenanceWindowList)
                tempMaintenanceWindows.Add(new MaintenanceWindow {from = mainWin.from, to = mainWin.to, abbreviation = mainWin.abbreviation, description = mainWin.description, hosts = mainWin.hosts });

I compare the two like so:

if (!tempMaintenanceWindows.SequenceEqual((List<MaintenanceWindow>)mainWinList.ItemsSource))

but the result of the SequenceEqual always seems to be false, although when debugging the code, they seem to be the exact same thing.

Hope someone can help. Thanks.


Quentin Roger provided an approach solution which works but I want to post my code which is probably not the neatest way to do it but it fits to my case of application.

So this is how I overrode the Equals method of my MaintenanceWindow object:

public override bool Equals (object obj)
        {
            MaintenanceWindow item = obj as MaintenanceWindow;

            if (!item.from.Equals(this.from))
                return false;
            if (!item.to.Equals(this.to))
                return false;
            if (!item.description.Equals(this.description))
                return false;
            if (!item.abbreviation.Equals(this.abbreviation))
                return false;
            if (item.hosts != null)
            {
                if (!item.hosts.Equals(this.hosts))
                    return false;
            }
            else
            {
                if (this.hosts != null)
                    return false;
            }

            return true;
        }

By default SequenceEqual will compare each item calling the equal function, do you override the equal? Otherwise it compares the memory adress for a class.

Also I encourage you to use FSharpList when you are looking for immutable list comparison.

"So in the override method do I have to compare every single field of my MaintenanceWindow class"

You have to compare every meaningful fields, yes.

If you declare your MaintenanceWindow like this :

As I said in my comment you have to compare every significant fields.In the following implementation I chose description, so if two MaintenanceWindow got the same description they ll be considered equals and the SequenceEquals ll work as expected.

 internal class MaintenanceWindow
 {
    public object @from { get; set; }
    public object to { get; set; }
    public object abbreviation { get; set; }

    private readonly string _description;
    public string Description => _description;

    public MaintenanceWindow(string description)
    {
        _description = description;
    }

    public string hosts { get; set; }

    public override bool Equals(object obj)
    {
        return this.Equals((MaintenanceWindow)obj);
    }

    protected bool Equals(MaintenanceWindow other)
    {
        return string.Equals(_description, other._description);
    }

    public override int GetHashCode()
    {
        return _description?.GetHashCode() ?? 0;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM