3

我想比较两个看起来像这样的类

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 ?:)

或者也许将其转换为列表?我不知道?:P

谢谢你的回答

4

3 回答 3

3

我想记录是否存在差异并记录类似这样的差异 CustomerNumber: changed from 456 to 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);
            }
        }
    }
}
于 2012-11-21T10:06:12.847 回答
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);
    }

}
于 2012-11-21T10:04:32.897 回答
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
  }
}

这可能是错误的,但根据我对您问题的理解,这就是您想要做的。

于 2012-11-21T10:06:19.630 回答