开发者

Not Equal in LINQ?

Table Reords:

TableA

SerialNUmber   Status
001              OK
002              N/A

TableB

SerialNumber    Status
001              OK
003              N/A

  var ret = (from p in TableA.AsEnumerable()
      join q in TableB.AsEnumerable()
       on p.Field<string>("SerialNumber") equals q.Field<string>("SerialNumber") 
          select new
            {
              SerialNUmber = q["SerialNumber"],
              UnitStatus = "HOT",
              PartNumber = q["PartNumber"]
            }).ToL开发者_如何学Goist();

ret

SerialNumber    Status
001              OK

This Query Returns the Match Data on My List. Now I need to do the other Way around to show the Serials that is not equal with one another.

ret

SerialNumber    Status
002              N/A
003              N/A

Thanks in Advance!


You could implement an EqualityComparer like this:

public class SerialNumberComparer : IEqualityComparer<DataRow>
{
  public bool Equals(Datarow d1, DataRow d2)
  {
    return d1.Field<string>("SerialNumber") == d2.Field<string>("SerialNumber");
  }
}

And then use the LINQ extension method Except:

var aEnum = TableA.AsEnumerable();
var bEnum = TableB.AsEnumerable();
var comp = new SerialNumberComparer();
var ret = from q in bEnum.Except(aEnum, comp).Concat(aEnum.Except(bEnum, comp))
select new
         {
           SerialNumber = q["SerialNumber"],
           UnitStatus = "HOT",
           PartNumber = q["PartNumber"]
         };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜