-1

I stuck to find duplicates from 2 datatables where the condition is dynamic.

Let's say TABLEA and TABLEB has same schema

and comparison columns could be anything. Columns could be more than one.

how can i build a dynamic condition to remove duplicate from TABLEA.

here i tried this query

e.g. i have column name with comma seperated

dynamiccolumnA, dynamiccolumnB, dynamiccolumnC

     var matched = from table1 in dt1.AsEnumerable()
           join table2 in dt2.AsEnumerable() on 
         table1.Field<object>(dynamiccolumn) equals table2.Field<object>(dynamiccolumn)
         where table1.Field<object>(dynamiccolumn) != table2.Field<object>(dynamiccolumn)
         select table1;

" where table1.Field(dynamiccolumn) != table2.Field(dynamiccolumn)" this statment could be for more than one column.

Could anybody gives me some pointer regarding this.

Thanks in advance

martolu
  • 31
  • 2
  • 6
  • Without any test, I'm pretty sure you can do it with Reflexion: `.Where(x => x.GetType().GetProperty(myColumnName).GetValue(x) != );` – Drag and Drop Jan 18 '18 at 08:26
  • But this will throw exception if the column does not exist. – Drag and Drop Jan 18 '18 at 08:27
  • Surprising, I realize, but you are not the first person in Stack Overflow history to want to join two tables on columns that are known only at run-time. See marked duplicates for some examples, include use of expressions and even a provided library (Dynamic LINQ). – Peter Duniho Jan 19 '18 at 00:04

1 Answers1

0

Looking at your problem, you need a query which can be customized without having the source code recompiled. I would suggest you to use expression trees. They are tough to understand and can bring complexity but its well worth the price.

Take a look at this article Microsoft Article on Expression Trees

Jas
  • 419
  • 1
  • 6
  • 14