Compare 2 Collections and get Modified items - c#

I have two ObservableCollection in my application, one which contain the items of type StoreSettings
named "oldSettings" anothe collection named "_stores".
public ObservableCollection<StoreSettings> oldSettings;
private ObservableCollection<StoreSettings> _stores;
Here is my StoreSettings class
public class StoreSettings :INotifyPropertyChanged
{
private bool _autoAOD;
public bool AutoAOD
{
get { return _autoAOD; }
set { _autoAOD = value;
}
private bool _autoGRN;
public bool AutoGRN
{
get { return _autoGRN; }
set { _autoGRN = value;
}
private bool _directPurchase;
public bool DirectPurchase
{
get { return _directPurchase; }
set { _directPurchase = value;
}
private decimal _gustoreID;
public decimal GUStoreID
{
get { return _gustoreID; }
set { _gustoreID = value;
}
private string _storeCode;
public string Storecode
{
get { return _storeCode; }
set { _storeCode = value;
}
I am updating some of the item's properties through my application, how can i find the modified items
through linq?
this is what i have tried, but it always gives count "0"
List<StoreSettings> result = _vmStoreconfig.oldSettings.Except(_vmStoreconfig.Stores).ToList();

If these two lists contain the same object instances, then it won't work because changes to object properties in one list will be applied to the other list also (since they are the same instances).
This means that you have to either:
clone objects before changing them, or
create a new instance on each change.
If they are not the same instances (i.e. if they are cloned or came through a database roundrip), then you need to provide a way for the runtime to compare individual properties.
You can either override the Equals method of the StoreSettings, or use a custom equality comparer for your StoreSettings class.
Something like:
public class StoreSettingsEqualityComparer : IEqualityComparer<StoreSettings>
{
public bool Equals(StoreSettings x, StoreSettings y)
{
if (object.ReferenceEquals(x, null))
return object.ReferenceEquals(y, null);
return
x.AutoAOD == y.AutoAOD &&
x.AutoGRN == y.AutoGRN &&
...
}
public int GetHashCode(StoreSettings obj)
{
unchecked
{
var h = 31;
h = h * 7 + obj.AutoAOD.GetHashCode();
...
return h;
}
}
}
And then use an overload of Enumerable.Except which accepts a custom comparer:
var comparer = new StoreSettingsEqualityComparer();
var results = first.Except(second, comparer).ToList();

This should get you the changes
List<StoreSettings> changes = _vmStoreconfig.oldSettings.FindAll(delegate(StoreSettings item1)
{
StoreSettings found = _vmStoreconfig.Stores.Find(delegate(StoreSettings item2) {
// Specify comparisons between properties here
return item2.propertyA == item1.propertyA ...;
}
return found != null;
});

Related

When using GetPosition for ArrayAdapter, Object with Equal Fields Return UnEqual

I have created a dropdown in Android Xamarin from which I would like to auto select a dropdown value on page load. When the page is launched, it is passed a MyClass object with a Num value of 52. My dropdown has been passed an ArrayAdapter which has a list of MyClass objects, one of which has a Num value of 52. When I try to mySpinner.SetSelection(recommendedPosition); it is not working because myClassAdapter.GetPosition(recommendedValue) returns -1
I thought that the example in the following article (which shows me how to override the Equals and Hash functions of MyClass) would help me, but it still results in -1 being returned. It seems that these overridden functions are not being hit when I place a breakpoint on them.. but I understood that the GetPosition method calls IndexOf which should result in my overriden functions being called.
https://www.javaworld.com/article/3305792/comparing-java-objects-with-equals-and-hashcode.html
In this example, MyClass has one field of Num.. but my code has many more properties in MyClass, all of which are equal between recommendedValue and one of the items in listMyClass.
In InstantiateItem of my ViewPager I call:
var recommendedValue = new MyClass("52");
var List<MyClass> listMyClass = new List<MyClass> { new MyClass("52"), new MyClass("46") };
mySpinner = view.FindViewById<Spinner>(Resource.Id.mySpinner);
var myClassAdapter = new MyClassAdapter(view.Context, listMyClass);
mySpinner.Adapter = myClassAdapter;
//*
// I need the following to not return -1
//*
var recommendedPosition = myClassAdapter.GetPosition((MyClass)recommendedValue);
MyClass.cs
public class MyClass: Java.Lang.Object
{
private string Num { get; set; } = "";
public MyClass() {}
public MyClass(int? num)
{
Num = String.IsNullOrEmpty(num) ? "" : num;
}
public string GetNum()
{
return Num;
}
public override bool Equals(object other)
{
return Equals(other as MyClass);
}
public bool Equals(MyClass otherItem)
{
if (otherItem == null)
{
return false;
}
return otherItem.Num == Num;
}
public override int GetHashCode()
{
int hash = 19;
hash = hash * 31 + (Num == null ? 0 : Num.GetHashCode());
return hash;
}
}
Maybe you could consider not using GetPosition method,you could directly get the position like this :
var recommendedValue = new MyClass("52");
var List<MyClass> listMyClass = new List<MyClass> { new MyClass("52"), new MyClass("46") };
var recommendedPosition = listMyClass.IndexOf(recommendedValue);
or you could define a GetSelectPosition(MyClass myClass) in your MyClassAdapter :
class MyClassAdapter: ArrayAdapter<MyClass >
{
public Context context;
public List<MyClass> list;
...
public int GetSelectPosition(MyClass myClass)
{
return list.IndexOf(myClass);
}
}
then you could call like :
var recommendedPosition = myClassAdapter.GetSelectPosition(recommendedValue);

Check if Dictionary contains key by comparing with different type

So what I want to do is this
var result = dictionary.ContainsKey(Guid.Empty);
Where dictionary is defined as var dictionary = new Dictionary<FooKeyClass, int>();
Right now FooKeyClass is basically just some data with a public property of type Guid.
I have tried to override Equals, I've tried writing my own IComparer, I've tried to inherit IEquatable<Guid>. What ever I do I can't seem to get the desired functionality. Can someone please tell me if this even is possible in C# and if so how do I go by implementing it?
Here is the rest of the code, its kinda bloted with overrides as it is now though:
public class FooKeyClass : IEquatable<Guid>
{
public Guid Guid { get; set; }
public string Name { get; set; }
public bool Equals(Guid guid)
{
if (guid == null)
return false;
return Guid.Equals(guid);
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
var p = obj as FooKeyClass;
if ((object)p == null)
return false;
return p.Guid.Equals(this.Guid);
}
public static bool operator ==(FooKeyClass a, Guid b)
{
if (object.ReferenceEquals(a, b))
return true;
if (((object)a == null) || ((object)b == null))
return false;
return a.Guid.Equals(b);
}
public static bool operator ==(FooKeyClass a, FooKeyClass b)
{
if (System.Object.ReferenceEquals(a, b))
return true;
if (((object)a == null) || ((object)b == null))
return false;
return a.Guid.Equals(b.Guid);
}
public static bool operator !=(FooKeyClass a, FooKeyClass b)
{
return !(a == b);
}
public static bool operator !=(FooKeyClass a, Guid b)
{
return !(a == b);
}
public override int GetHashCode()
{
return Guid.GetHashCode();
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var dictionary = new Dictionary<FooKeyClass, int>();
var savedGuid = Guid.NewGuid();
var tmpKey = new FooKeyClass() { Guid = savedGuid, Name = "feeefeee" };
dictionary.Add(tmpKey, 42);
var result = tmpKey.Equals(savedGuid); // no error
result = tmpKey == savedGuid; // no error
result = dictionary.ContainsKey(savedGuid); // compile errror
result = dictionary.Contains(savedGuid); // compile errror
result = dictionary.Contains<Guid>(savedGuid); // compile errror
}
}
Your two primary options are the following:
Use the Guid to create an instance of FooKeyClass that you use as the actual key:
var result = dictionary.ContainsKey(new FooKeyClass(Guid.Empty));
Change the type of your dictionary to Dictionary<Guid, int>.
If you frequently use Guid.Empty as the key for some particular purpose, you can create the equivalent key FooKeyClass.Empty so you don't have to keep creating new instances of it.
Note that since your FooKeyClass is used as the key for a dictionary, you should make sure the result of your GetHashCode method cannot change after an instance is created. In your current implementation, if you set the FooKeyClass.Guid property after the key is added to a dictionary, the entry in the dictionary will be "lost" because the hash code changes. This situation is automatically avoided if you use Guid instead of FooKeyClass as the keys of your dictionary, or you can remove the setter for the FooKeyClass.Guid property and require the user to use a constructor instead.

C# Linq, Searching for same items in two lists

we have the following setup:
We have a array of objects with a string in it (xml-ish but not normalized) and we have a list/array of strings with id.
We need to find out if a string from that list with id's is also pressent in one of the objects.
Here we have a setup that we have tried:
public class Wrapper
{
public string MyProperty { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Wrapper> wrappers = new List<Wrapper>()
{
new Wrapper{ MyProperty = "<flkds,dlsklkdlsqkdkqslkdlqk><id>3</id><sqjldkjlfdskjlkfjsdklfj>"},
new Wrapper{ MyProperty = "<flkds,dlsklkdlsqkdkqslkdlqk><id>2</id><sqjldkjlfdskjlkfjsdklfj>"}
};
string[] ids = { "<id>0</id>", "<id>1</id>", "<id>2</id>" };
var props = wrappers.Select(w => w.MyProperty);
var intersect = props.Intersect(ids, new MyEquilityTester());
Debugger.Break();
}
}
class MyEquilityTester: IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
return x.Contains(y);
}
public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
Edit:
What we expect is when we do a .Any() on intersect that is says true because wrappers has a object with a prop that contains <id>2</id>, intersect is null.
If we are using the wrong method please say. It should work as fast as posible. A simple true when found will do!
For your case, you could write your IEqualitycomparer like this:
class MyEquilityTester: IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
return x.Contains(y) || y.Contains(x);
}
public int GetHashCode(string obj)
{
return 0;
}
}
and it will find
<flkds,dlsklkdlsqkdkqslkdlqk><id>2</id><sqjldkjlfdskjlkfjsdklfj>
This works because GetHashCode always return 0, and the x.Contains(y) || y.Contains(x) check.
Another not-so-hacky solution is to use a Where in combination with Any
IEnumerable<String> intersect = props.Where(p => ids.Any (i => p.Contains(i)));
or replace the Where with another Any if you don't care about the actual items and you only want a true or false.
bool intersect = props.Any(p => ids.Any (i => p.Contains(i)));
wrappers.Where(w=>ids.Any(i=>w.MyProperty.Contains(i)))

How to compare, in C#, two lists of objects on one or more properties of these objects?

First of all I must say I'm not a seasoned programmer. I looked at similar problems on StackOverflow but didn't seem to find a suitable answer that I can implement with my limited skills.
In C#, I need to compare two lists of objects based on the values of one or more properties in those objects. I want to create two new lists, one of the objects that exist in the left, but have differences in some property values in, or don't exist at all in the right list and vice versa.
Before I only had to compare the two based on one value, so I did not have to work on objects but on string, so I did something like this:
(LeftItems and RightItems are Entities)
List<String> leftList = new List<string>();
List<String> rightList = new List<string>();
List<String> leftResultList = new List<string>();
List<String> rightResultList = new List<string>();
List<String> leftResultObjList = new List<string>();
List<String> rightResultObjList = new List<string>();
foreach (item i in leftItems)
{
leftlist.Add(i.value);
}
//same for right
foreach (string i in leftList)
{
if(!rightList.contains(i))
{
leftResultList.Add(i);
}
}
//same for the right list
Now I have to compare on more than one value, so I created a class which has several properties that I need to compare, so I'd like to do the same as the above, but with object properties:
class CompItems
{
string _x;
string _y;
public CompItems(string x, string y)
{
_x = x;
_y = y;
}
}
foreach (item i in leftItems)
{
leftList.Add(new CompItem(i.value1,i.value2));
}
//same for the right list
foreach (CompItem c in leftItems)
{
// Here is where things go wrong
if(one property of object in rightItems equals property of object in leftItems) && some other comparisons
{
resultLeftObjList.Add(c)
}
}
//And the same for the right list
You can make your class inherit from IComparable and do the comparison based on the properties you want like the following:
class Employee : IComparable
{
private string name;
public string Name
{
get { return name; }
set { name = value ; }
}
public Employee( string a_name)
{
name = a_name;
}
#region IComparable Members
public int CompareTo( object obj)
{
Employee temp = (Employee)obj;
if ( this.name.Length < temp.name.Length)
return -1;
else return 0;
}
}
You can find the details of this solution here
The easiest and most OOP approach in this case, imo, could be a simple implementation
of IComparable Interface on your both types, and after simply call CompareTo.
Hope this helps.
For example override
public Coordinates(string x, string y)
{
X = x;
Y = y;
}
public string X { get; private set; }
public string Y { get; private set; }
public override bool Equals(object obj)
{
if (!(obj is Coordinates))
{
return false;
}
Coordinates coordinates = (Coordinates)obj;
return ((coordinates.X == this.X) && (coordinates.Y == this.Y));
}
And then call 'Equal' of list

How to use the IEqualityComparer

I have some bells in my database with the same number. I want to get all of them without duplication. I created a compare class to do this work, but the execution of the function causes a big delay from the function without distinct, from 0.6 sec to 3.2 sec!
Am I doing it right or do I have to use another method?
reg.AddRange(
(from a in this.dataContext.reglements
join b in this.dataContext.Clients on a.Id_client equals b.Id
where a.date_v <= datefin && a.date_v >= datedeb
where a.Id_client == b.Id
orderby a.date_v descending
select new Class_reglement
{
nom = b.Nom,
code = b.code,
Numf = a.Numf,
})
.AsEnumerable()
.Distinct(new Compare())
.ToList());
class Compare : IEqualityComparer<Class_reglement>
{
public bool Equals(Class_reglement x, Class_reglement y)
{
if (x.Numf == y.Numf)
{
return true;
}
else { return false; }
}
public int GetHashCode(Class_reglement codeh)
{
return 0;
}
}
Your GetHashCode implementation always returns the same value. Distinct relies on a good hash function to work efficiently because it internally builds a hash table.
When implementing interfaces of classes it is important to read the documentation, to know which contract you’re supposed to implement.1
In your code, the solution is to forward GetHashCode to Class_reglement.Numf.GetHashCode and implement it appropriately there.
Apart from that, your Equals method is full of unnecessary code. It could be rewritten as follows (same semantics, ¼ of the code, more readable):
public bool Equals(Class_reglement x, Class_reglement y)
{
return x.Numf == y.Numf;
}
Lastly, the ToList call is unnecessary and time-consuming: AddRange accepts any IEnumerable so conversion to a List isn’t required. AsEnumerable is also redundant here since processing the result in AddRange will cause this anyway.
1 Writing code without knowing what it actually does is called cargo cult programming. It’s a surprisingly widespread practice. It fundamentally doesn’t work.
Try This code:
public class GenericCompare<T> : IEqualityComparer<T> where T : class
{
private Func<T, object> _expr { get; set; }
public GenericCompare(Func<T, object> expr)
{
this._expr = expr;
}
public bool Equals(T x, T y)
{
var first = _expr.Invoke(x);
var sec = _expr.Invoke(y);
if (first != null && first.Equals(sec))
return true;
else
return false;
}
public int GetHashCode(T obj)
{
return obj.GetHashCode();
}
}
Example of its use would be
collection = collection
.Except(ExistedDataEles, new GenericCompare<DataEle>(x=>x.Id))
.ToList();
If you want a generic solution that creates an IEqualityComparer for your class based on a property (which acts as a key) of that class have a look at this:
public class KeyBasedEqualityComparer<T, TKey> : IEqualityComparer<T>
{
private readonly Func<T, TKey> _keyGetter;
public KeyBasedEqualityComparer(Func<T, TKey> keyGetter)
{
if (default(T) == null)
{
_keyGetter = (x) => x == null ? default : keyGetter(x);
}
else
{
_keyGetter = keyGetter;
}
}
public bool Equals(T x, T y)
{
return EqualityComparer<TKey>.Default.Equals(_keyGetter(x), _keyGetter(y));
}
public int GetHashCode(T obj)
{
TKey key = _keyGetter(obj);
return key == null ? 0 : key.GetHashCode();
}
}
public static class KeyBasedEqualityComparer<T>
{
public static KeyBasedEqualityComparer<T, TKey> Create<TKey>(Func<T, TKey> keyGetter)
{
return new KeyBasedEqualityComparer<T, TKey>(keyGetter);
}
}
For better performance with structs there isn't any boxing.
Usage is like this:
IEqualityComparer<Class_reglement> equalityComparer =
KeyBasedEqualityComparer<Class_reglement>.Create(x => x.Numf);
Just code, with implementation of GetHashCode and NULL validation:
public class Class_reglementComparer : IEqualityComparer<Class_reglement>
{
public bool Equals(Class_reglement x, Class_reglement y)
{
if (x is null || y is null))
return false;
return x.Numf == y.Numf;
}
public int GetHashCode(Class_reglement product)
{
//Check whether the object is null
if (product is null) return 0;
//Get hash code for the Numf field if it is not null.
int hashNumf = product.hashNumf == null ? 0 : product.hashNumf.GetHashCode();
return hashNumf;
}
}
Example:
list of Class_reglement distinct by Numf
List<Class_reglement> items = items.Distinct(new Class_reglementComparer());
The purpose of this answer is to improve on previous answers by:
making the lambda expression optional in the constructor so that full object equality can be checked by default, not just on one of the properties.
operating on different types of classes, even complex types including sub-objects or nested lists. And not only on simple classes comprising only primitive type properties.
Not taking into account possible list container differences.
Here, you'll find a first simple code sample that works only on simple types (the ones composed only by primitif properties), and a second one that is complete (for a wider range of classes and complex types).
Here is my 2 pennies try:
public class GenericEqualityComparer<T> : IEqualityComparer<T> where T : class
{
private Func<T, object> _expr { get; set; }
public GenericEqualityComparer() => _expr = null;
public GenericEqualityComparer(Func<T, object> expr) => _expr = expr;
public bool Equals(T x, T y)
{
var first = _expr?.Invoke(x) ?? x;
var sec = _expr?.Invoke(y) ?? y;
if (first == null && sec == null)
return true;
if (first != null && first.Equals(sec))
return true;
var typeProperties = typeof(T).GetProperties();
foreach (var prop in typeProperties)
{
var firstPropVal = prop.GetValue(first, null);
var secPropVal = prop.GetValue(sec, null);
if (firstPropVal != null && !firstPropVal.Equals(secPropVal))
return false;
}
return true;
}
public int GetHashCode(T obj) =>
_expr?.Invoke(obj).GetHashCode() ?? obj.GetHashCode();
}
I know we can still optimize it (and maybe use a recursive?)..
But that is working like a charm without this much complexity and on a wide range of classes. ;)
Edit: After a day, here is my $10 attempt:
First, in a separate static extension class, you'll need:
public static class CollectionExtensions
{
public static bool HasSameLengthThan<T>(this IEnumerable<T> list, IEnumerable<T> expected)
{
if (list.IsNullOrEmptyCollection() && expected.IsNullOrEmptyCollection())
return true;
if ((list.IsNullOrEmptyCollection() && !expected.IsNullOrEmptyCollection()) || (!list.IsNullOrEmptyCollection() && expected.IsNullOrEmptyCollection()))
return false;
return list.Count() == expected.Count();
}
/// <summary>
/// Used to find out if a collection is empty or if it contains no elements.
/// </summary>
/// <typeparam name="T">Type of the collection's items.</typeparam>
/// <param name="list">Collection of items to test.</param>
/// <returns><c>true</c> if the collection is <c>null</c> or empty (without items), <c>false</c> otherwise.</returns>
public static bool IsNullOrEmptyCollection<T>(this IEnumerable<T> list) => list == null || !list.Any();
}
Then, here is the updated class that works on a wider range of classes:
public class GenericComparer<T> : IEqualityComparer<T> where T : class
{
private Func<T, object> _expr { get; set; }
public GenericComparer() => _expr = null;
public GenericComparer(Func<T, object> expr) => _expr = expr;
public bool Equals(T x, T y)
{
var first = _expr?.Invoke(x) ?? x;
var sec = _expr?.Invoke(y) ?? y;
if (ObjEquals(first, sec))
return true;
var typeProperties = typeof(T).GetProperties();
foreach (var prop in typeProperties)
{
var firstPropVal = prop.GetValue(first, null);
var secPropVal = prop.GetValue(sec, null);
if (!ObjEquals(firstPropVal, secPropVal))
{
var propType = prop.PropertyType;
if (IsEnumerableType(propType) && firstPropVal is IEnumerable && !ArrayEquals(firstPropVal, secPropVal))
return false;
if (propType.IsClass)
{
if (!DeepEqualsFromObj(firstPropVal, secPropVal, propType))
return false;
if (!DeepObjEquals(firstPropVal, secPropVal))
return false;
}
}
}
return true;
}
public int GetHashCode(T obj) =>
_expr?.Invoke(obj).GetHashCode() ?? obj.GetHashCode();
#region Private Helpers
private bool DeepObjEquals(object x, object y) =>
new GenericComparer<object>().Equals(x, y);
private bool DeepEquals<U>(U x, U y) where U : class =>
new GenericComparer<U>().Equals(x, y);
private bool DeepEqualsFromObj(object x, object y, Type type)
{
dynamic a = Convert.ChangeType(x, type);
dynamic b = Convert.ChangeType(y, type);
return DeepEquals(a, b);
}
private bool IsEnumerableType(Type type) =>
type.GetInterface(nameof(IEnumerable)) != null;
private bool ObjEquals(object x, object y)
{
if (x == null && y == null) return true;
return x != null && x.Equals(y);
}
private bool ArrayEquals(object x, object y)
{
var firstList = new List<object>((IEnumerable<object>)x);
var secList = new List<object>((IEnumerable<object>)y);
if (!firstList.HasSameLengthThan(secList))
return false;
var elementType = firstList?.FirstOrDefault()?.GetType();
int cpt = 0;
foreach (var e in firstList)
{
if (!DeepEqualsFromObj(e, secList[cpt++], elementType))
return false;
}
return true;
}
#endregion Private Helpers
We can still optimize it but it worth give it a try ^^.
The inclusion of your comparison class (or more specifically the AsEnumerable call you needed to use to get it to work) meant that the sorting logic went from being based on the database server to being on the database client (your application). This meant that your client now needs to retrieve and then process a larger number of records, which will always be less efficient that performing the lookup on the database where the approprate indexes can be used.
You should try to develop a where clause that satisfies your requirements instead, see Using an IEqualityComparer with a LINQ to Entities Except clause for more details.
IEquatable<T> can be a much easier way to do this with modern frameworks.
You get a nice simple bool Equals(T other) function and there's no messing around with casting or creating a separate class.
public class Person : IEquatable<Person>
{
public Person(string name, string hometown)
{
this.Name = name;
this.Hometown = hometown;
}
public string Name { get; set; }
public string Hometown { get; set; }
// can't get much simpler than this!
public bool Equals(Person other)
{
return this.Name == other.Name && this.Hometown == other.Hometown;
}
public override int GetHashCode()
{
return Name.GetHashCode(); // see other links for hashcode guidance
}
}
Note you DO have to implement GetHashCode if using this in a dictionary or with something like Distinct.
PS. I don't think any custom Equals methods work with entity framework directly on the database side (I think you know this because you do AsEnumerable) but this is a much simpler method to do a simple Equals for the general case.
If things don't seem to be working (such as duplicate key errors when doing ToDictionary) put a breakpoint inside Equals to make sure it's being hit and make sure you have GetHashCode defined (with override keyword).

Categories