1

我有两个具有相同列数的数据表。我必须检查哪个名称在新数据表中有不同的组或子组。(Using Linq

DataTableOLD

CODE    NAME    GROUP   SUBGROUP
c1      AA      GP1     SGP1
c2      BB      GP1     SGP1
c3      CC      GP1     SGP2
c4      DD      GP1     SGP2
c5      EE      GP2     SGP3


DataTableNEW
CODE    NAME    GROUP   SUBGROUP
c1      AA      GP1     SGP4
c2      BB      GP1     SGP1
c3      CC      GP3     SGP5
c4      DD      GP1     SGP2
c6      FF      GP2     SGP3


Resultant table (show only those where there is mismatch in either group or group and subgroup both)

OLDCODE OLDNAME OLDGROUP OLDSUBGROUP  NEWCODE NEWNAME NEWGROUP NEWSUBGROUP
c1      AA      GP1         SGP1        c1      AA      GP1     SGP4
c3      CC      GP1         SGP2        c3      CC      GP3     SGP5
4

1 回答 1

1
var lstResult = (from _old in DataTableOLD.AsEnumerable()
    join _new in DataTableNew.AsEnumerable() on _old.Field<string>("CODE") 
      equals _new.Field<string>("CODE")
    where _old.Field<string>("GROUP") != _new.Field<string>("GROUP") ||
    _old.Field<string>("SUBGROUP") != _new.Field<string>("SUBGROUP")
    select new
    {
        OLDCODE = _old.Field<string>("CODE"),
        OLDNAME = _old.Field<string>("NAME"),
        OLDGROUP = _old.Field<string>("GROUP"),
        OLDSUBGROUP = _old.Field<string>("SUBGROUP"),
        NEWCODE = _new.Field<string>("CODE"),
        NEWNAME = _new.Field<string>("NAME"),
        NEWGROUP = _new.Field<string>("GROUP"),
        NEWSUBGROUP = _new.Field<string>("SUBGROUP")
     }
);
于 2013-09-24T09:37:05.120 回答