我想比较两个看起来像这样的类
public class Order
{
public string KundNummer, KundNamn, ErReferens, VarReferens, KontraktsNummer, Betvillk, Levvillk, Levsatt, Speditor,
Resenh, Projekt, OrderDatum, LeveransDatum, ErtOrdernr, LagerbokfDatum, KundPostAdr, KundPostAdr2, KundGLN, KundPostnr,
KundOrt, KundLandKod, KundLand, KundVATnr, KundDistrikt, KundSaljare, KundText1, KundText2, KundText3, KundSprak,
KundValuta, KundRabattAvtal, KundRabattAvtalBenamning, KundPrislista, KundPrislistaBenamning, KundALnamn, KundALpostAdr, KundALpostAdr2, KundALGLN, KundALpostnr, KundALort,
KundALlandKod, KundALland;
public double OrderNummer, Fakturarabatt, Frakt, Expavg, Brutto, Netto, ExklMoms, Totalt, Moms, Avrundn, KundValutaKurs, KundValutaEnhet;
public int EUPeriodSamman, InklMoms, EjKlar, Levererad, Malukerad, BestallningSkapad, Ordererk,
Plocklista, Foljesedel, ExtraOrderdokument, Restorder, Faktura, KundSparaText, KundExport, KundRantefakturering, KundKravbrev,
KundKravavgift, KundRestnoteraEj, KundSamlingsfakturera;
}
我想比较这个类的两个对象,以记录我的数据库中哪些字段发生了变化。
public string OrderUppdateraOrder(Order order)
{
Order OrderToCompare = new Order();
OrderToCompare = OrderVisaOrderInformation(order.OrderNummer);
//then the code goes on to make the changes to the database from the class order
//while OrdertoComapre still have the values from before this function was called
}
那么可以循环遍历这些类并比较它们,还是我必须为我班级中的每个变量写一个 if ? :)
或者可能将其转换为List?我不知道? :P
感谢您的回答
答案 0 :(得分:3)
我想记录是否存在差异并记录此CustomerNumber之类的差异:从456更改为345
最好的选择可能只是使用反射来抓取公共属性和字段,并进行比较。这将比较少的代码,但会产生性能开销。使用FastMember等工具可以大大减少开销:
static class Program {
static void Main()
{
Order o1 = new Order { Resenh = "abc" },
o2 = new Order { Resenh = "abc" };
ShowDiffs(o1, o2); // {nothing}
o2.Resenh = "def";
ShowDiffs(o1, o2); // Resenh changed from abc to def
}
static void ShowDiffs<T>(T x, T y)
{
var accessor = TypeAccessor.Create(typeof(T));
if (!accessor.GetMembersSupported) throw new NotSupportedException();
var members = accessor.GetMembers();
foreach (var member in members)
{
object xVal = accessor[x, member.Name],
yVal = accessor[y, member.Name];
if (!Equals(xVal, yVal))
{
Console.WriteLine("{0} changed from {1} to {2}",
member.Name, xVal, yVal);
}
}
}
}
答案 1 :(得分:1)
也许这种方法可以帮助你
public class Order
{
public string KundNummer, KundNamn, ErReferens, VarReferens, KontraktsNummer, Betvillk, Levvillk, Levsatt, Speditor,
Resenh, Projekt, OrderDatum, LeveransDatum, ErtOrdernr, LagerbokfDatum, KundPostAdr, KundPostAdr2, KundGLN, KundPostnr,
KundOrt, KundLandKod, KundLand, KundVATnr, KundDistrikt, KundSaljare, KundText1, KundText2, KundText3, KundSprak,
KundValuta, KundRabattAvtal, KundRabattAvtalBenamning, KundPrislista, KundPrislistaBenamning, KundALnamn, KundALpostAdr, KundALpostAdr2, KundALGLN, KundALpostnr, KundALort,
KundALlandKod, KundALland;
public double OrderNummer, Fakturarabatt, Frakt, Expavg, Brutto, Netto, ExklMoms, Totalt, Moms, Avrundn, KundValutaKurs, KundValutaEnhet;
public int EUPeriodSamman, InklMoms, EjKlar, Levererad, Malukerad, BestallningSkapad, Ordererk,
Plocklista, Foljesedel, ExtraOrderdokument, Restorder, Faktura, KundSparaText, KundExport, KundRantefakturering, KundKravbrev,
KundKravavgift, KundRestnoteraEj, KundSamlingsfakturera;
public static bool operator ==(Order left, Order right)
{
foreach (var field in left.GetType().GetFields())
{
object valueLeft = field.GetValue(left);
object valueRight = field.GetValue(right);
if (!object.Equals(valueLeft, valueRight))
return false;
}
return true;
}
public static bool operator !=(Order left, Order right)
{
return !(left == right);
}
}
答案 2 :(得分:1)
根据我的理解,您可以使用一种方法从数据库中检索Order
对象,并从数据库中获取一个Order
对象,但需要进行修改。你想检查一个或多个订单是否有变化而不说:
如果Database.A等于Your.A等,则为每个订单
我会将您的支票放在一个地方并覆盖Equals
方法。
public class Order
{
public int Id;
public string SomeProp;
public string AnotherProp;
public override bool Equals(object obj)
{
Order orderToCompare = (Order)obj;
return (SomeProp == orderToCompare.SomeProp && AnotherProp == orderToCompare.AnotherProp);
}
}
然后有两个方法,一个用于更新单个订单,另一个用于更新多个订单。
private void UpdateOrder(Order o)
{
//Get the corresponding order from the database (I suspect Entity Framework here in order to get an object?)
Order dbOrder = OrderVisaOrderInformation(o.Id);
if (dbOrder.Equals(o))
{
//Do some update
}
}
private void UpdateManyOrders(List<Order> orders)
{
var dbOrders = (from order in orders
select OrderVisaOrderInformation(order.Id));
List<Order> ordersToUpdate = dbOrders.Where(x => !x.Equals(orders.First(y => y.Id == x.Id))).ToList();
foreach (Order orderToUpdate in ordersToUpdate)
{
//Update the order
}
}
这可能是错的,但从我如何理解你的问题,这就是你想要做的。