Created
March 20, 2021 10:25
-
-
Save mzuvin/25f9e21678e0758c7983a153512a5cab to your computer and use it in GitHub Desktop.
ChangeTracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://stackoverflow.com/a/65642075/9218468 | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ChangeTracker | |
{ | |
public class ChangeTracker<T1, T2> | |
{ | |
private readonly IEnumerable<T1> oldValues; | |
private readonly IEnumerable<T2> newValues; | |
private readonly Func<T1, T2, bool> areEqual; | |
public ChangeTracker(IEnumerable<T1> oldValues, IEnumerable<T2> newValues, Func<T1, T2, bool> areEqual) | |
{ | |
this.oldValues = oldValues; | |
this.newValues = newValues; | |
this.areEqual = areEqual; | |
} | |
public IEnumerable<T2> AddedItems | |
{ | |
get => newValues.Where(n => oldValues.All(o => !areEqual(o, n))); | |
} | |
public IEnumerable<T1> RemovedItems | |
{ | |
get => oldValues.Where(n => newValues.All(o => !areEqual(n, o))); | |
} | |
public IEnumerable<T1> UpdatedItems | |
{ | |
get => oldValues.Where(n => newValues.Any(o => areEqual(n, o))); | |
} | |
} | |
class Program | |
{ | |
static bool AreEqual(string a, string b) | |
{ | |
if (a == null && b == null) | |
return true; | |
if (a == null || b == null) | |
return false; | |
return a == b; | |
} | |
static void Main(string[] args) | |
{ | |
List<string> list = new List<string>(); | |
list.Add("mustafa"); | |
list.Add("demozart"); | |
List<string> list2 = new List<string>(); | |
list2.Add("demozart"); | |
list2.Add("oguz"); | |
var changesTracker = new ChangeTracker<string, string>(list, list2,AreEqual); | |
Console.WriteLine("Eklenen"); | |
Console.WriteLine(String.Join(", ", changesTracker.AddedItems)); | |
Console.WriteLine("Değişen"); | |
Console.WriteLine(String.Join(", ", changesTracker.UpdatedItems)); | |
Console.WriteLine("Silinen"); | |
Console.WriteLine(String.Join(", ", changesTracker.RemovedItems)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment