-1

I have two DataSet A and B in .Net. Ds A and B has Columns ColA and ColB in both.

I need to check if any corresponding First and Second rows of A are available in B. It should return only the Rows from A which are not in B.

It should basically do this.

Not(Select ColA, ColB from DsA [join DsB ? or self join?] 
where DsA.ColA == DsB.ColA and DsA.ColB == DsB.ColB)

DsA

(ColA,ColB) - (1, 10), (1, 11), (2, 12), (3,13), (4, 14)

DsB

(ColA,ColB) - (1, 9), (1, 10), (2, 12), (3,15)

The query should return (1, 11), (3, 13), (4, 14) {from DsA}.

Sorry for the format. I am new here and I do not know how to make a proper table.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Laba Ningombam
  • 105
  • 1
  • 2
  • 7
  • What are "corresponding First and Second rows"? – Tim Schmelter May 29 '18 at 09:32
  • Possible duplicate of [How to use linq \`Except\` with multiple properties with different class?](https://stackoverflow.com/questions/28782437/how-to-use-linq-except-with-multiple-properties-with-different-class) – Hasan Gholamali May 29 '18 at 09:33

2 Answers2

0

You can use a LINQ-LEFT-OUTER-JOIN

var rowsOnlyInA = 
    from a in A.Tables[0].AsEnumerable()
    join b in B.Tables[0].AsEnumerable() 
    on     new{ ColA = a.Field<string>("ColA"), ColB = a.Field<string>("ColB") }
    equals new{ ColA = b.Field<string>("ColA"), ColB = b.Field<string>("ColB") } into ps
    from p in ps.DefaultIfEmpty()
    where p == null
    select a;

if(rowsOnlyInA.Any())
{
   DataTable resulTable = rowsOnlyInA.CopyToDataTable();
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Hi @Tim Schmelter, Thanks for your help. The problem with me is, I do not have any Primary key,in the two Dataset to make the Join On statement. I have updated my question if you can look at it. – Laba Ningombam May 29 '18 at 10:12
  • @LabaNingombam: no primary key is necessary with my LINQ approach. You see that i join on those two columns – Tim Schmelter May 29 '18 at 10:42
0

You could try using IEqualityComparer

private static void Find()
{
    var uniqueDataSets = dataSet1.Except(dataSet2, new DataComparer());
}


class DataComparer : IEqualityComparer<DataSet>
{
    public bool Equals(DataSet x, DataSet y)
    {
        if (object.ReferenceEquals(x, y)) return true;

        return x?.ColA == y?.ColA && x?.ColB == y?.ColB;
    }

    public int GetHashCode(DataSet obj)
    {
        if (obj == null) return 0;

        return obj.ColA.GetHashCode() ^ obj.ColB.GetHashCode();
    }
}
Alen Alex
  • 897
  • 4
  • 15
  • 31