0

I have TableA and TableB, where TableA has composite primary key which is referenced in TableB. Composite columns are available in string array. How can I get rows of TableB matching with those of TableA using columns in string array which comes dynamically where TableA and TableB are loaded loosly in dataset.

Say e.g.

string[] arTableA = new string[]{Composite key columns of A};
string[] arTableB = new string[]{Composite key columns of B};

var Result = (from A in TableA)
              join B in TableB on new {arTableA } equals new {arTableB}  
              select TableB.Columns
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Prashant
  • 3
  • 2
  • You say `DataSet`, is it really a Linq-To-Objects `DataSet(in memory collection) or it it a database provider? – Tim Schmelter Oct 16 '17 at 07:59
  • Possible duplicate of [How to do joins in LINQ on multiple fields in single join](https://stackoverflow.com/questions/373541/how-to-do-joins-in-linq-on-multiple-fields-in-single-join) – qxg Oct 16 '17 at 08:04
  • Thanks for quick reply. Tables are in Dataset and I'm iterating over it. The tables has data like order and orderdetails. By some rule I have to remove some order and in cascading effect orderdetails rows. The problem is I need to find unique columns first from both tables then based on these columns I have to remove columns from second table. The uniques columns are collected in string array. – Prashant Oct 16 '17 at 09:40

1 Answers1

0

Make a function that would concatenate key values like this:

var key = new Func<DataRow, string[], string>((r, keys) => {
    return keys.Select(c => r[c].ToString()).Aggregate((current, next) => current + next);
});

You would than use that function in your query when joining like this:

var result = from A in GetTableA().AsEnumerable()
             join B in GetTableB().AsEnumerable() on key(A, arTableA) equals key(B, arTableB)
             select B;
Robert
  • 2,407
  • 1
  • 24
  • 35