i'm using ef4.3 i'm referring entities, apply class containing properties.
i'm trying figure out if possible compare 2 entities. each entity has properties assigned values clarity let entity 'customer'.
public partial class customer { public string name { get; set; } public datetime dateofbirth { get; set; } ... ... }
the customer visits website , types in details 'typedcustomer'. check against database , if of data matches, return record database 'storedcustomer'.
so @ point i've identified same customer returning wan't valid rest of data. check each property 1 one, there fair few check. possible make comparison @ higher level takes account current values of each?
if(typedcustomer == storedcustomer) { .... }
if you're storing these things in database, logical assume you'd have primary key called id
.
public partial class customer { public int id { get; set; } public string name { get; set; } public datetime dateofbirth { get; set; } ... ... }
then is:
if(typedcustomer.id == storedcustomer.id) { }
update:
in project, have comparer these circumstances:
public sealed class pococomparer<tpoco> : iequalitycomparer<tpoco> tpoco : class { public bool equals(tpoco poco1, tpoco poco2) { if (poco1 != null && poco2 != null) { bool aresame = true; foreach(var property in typeof(tpoco).getpublicproperties()) { object v1 = property.getvalue(poco1, null); object v2 = property.getvalue(poco2, null); if (!object.equals(v1, v2)) { aresame = false; break; } }); return aresame; } return poco1 == poco2; } // eo equals public int gethashcode(tpoco poco) { int hash = 0; foreach(var property in typeof(tpoco).getpublicproperties()) { object val = property.getvalue(poco, null); hash += (val == null ? 0 : val.gethashcode()); }); return hash; } // eo gethashcode } // eo class pococomparer
uses extension method:
public static partial class typeextensionmethods { public static propertyinfo[] getpublicproperties(this type self) { self.throwifdefault("self"); return self.getproperties(bindingflags.public | bindingflags.instance).where((property) => property.getindexparameters().length == 0 && property.canread && property.canwrite).toarray(); } // eo getpublicproperties } // eo class typeextensionmethods
Comments
Post a Comment