using System.Collections.Generic;
private List<SomeDto> Data = new List<SomeDto>();
private List<SomeDto> OriginalData = new List<SomeDto>();
Console.WriteLine("Original list: ");
foreach(var dto in OriginalData)
Console.WriteLine(dto.ToString());
Console.WriteLine("Changed list: ");
Console.WriteLine(dto.ToString());
OriginalData = new List<SomeDto>
new SomeDto { Id = 1, Code = "T1", Name = "Test1", Description = "First" },
new SomeDto { Id = 2, Code = "T2", Name = "Test2", Description = "Second" },
new SomeDto { Id = 3, Code = "T3", Name = "Test3", Description = "Third" },
new SomeDto { Id = 4, Code = "T4", Name = "Test4", Description = "Forth" }
Data = OriginalData.GetCopy();
private void ChangeData()
if(dto.Id != 2 && dto.Id != 4)
dto.Code = "Code" + dto.Id.ToString();
dto.Name = "Changed " + dto.Id.ToString();
private void RemoveUnchanged()
Data.RemoveAll(x => OriginalData.Any(y => y.Equals(x)));
public class SomeDto : IDtoCloneable<SomeDto>
public int Id { get; set;}
public string Code { get; set; }
public string Name { get; set; }
public string Description { get; set;}
public override string ToString()
return "Id: " + Id + ", Code: " + Code + ", Name: " + Name + ", Description: " + Description;
public override bool Equals(object obj)
public bool Equals(SomeDto obj)
return obj.Id == Id && obj.Code == Code && obj.Name == Name && obj.Description == Description;
public static class ListExtensions
public static List<T> GetCopy<T>(this IList<T> originalList) where T : class, IDtoCloneable<T>, new()
return originalList.Select(x => x.GetClone()).ToList();
public interface IDtoCloneable<out T> : ICloneable where T : class, new()
object ICloneable.Clone()
return MemberwiseClone();