This project is read-only.

Compare sequences

Simple Comparison
var list1 = new[] {"green", "blue"};
var list2 = new[] {"blue", "red", "green"};

var result = list2.Compare(list1);

//result.Added should contain "red"

Discussion
The result will hold the items that have been added or removed

Special Comparison
This extension method was created for very specific cases.
Let's consider the following scenario: We have a group of users and we want to bulk manage them (not the perfect example... ). So we can add , remove or rename some users. We want to see what changed compared to the previous saved group of users.
public class User:IEquatable<User>
{
public int Id { get; set; }
public string Name { get; set; }
public bool Equals(User other)
{
	return Id == other.Id;
}
}
var alex = new User() {Id = 1, Name = "Alex"};
var george = new User() {Id = 2, Name = "George"};
var mary = new User() {Id = 3, Name = "Mary"};
var stewie = new User {Id = 5, Name = "Stewie"};
var john = new User {Id = 2, Name="john"}; 
var list1 = new[] {alex, george, mary,stewie}; //the saved user list
var list2 = new[] {john,alex, mary}; // the new user list

var result=list2.WhatChanged(list1,(recent,old)=> recent.Name!=old.Name);

Discussion
We see that initially we have 4 users. After doing administrative work we have 3 users (stewie and george are missing). But we also we notice that john and george have the same Id, so george has been renamed to john.
WhatChanged method will detect what was added/removed or changed. That's why the second parameter is a delegate to determine if the content (in this example the name of user) has changed. This method is for use with entities( objects that have an identity).

Last edited Jan 21, 2011 at 8:45 AM by mike_sapiens, version 2

Comments

No comments yet.