0

I have a linq statements that takes two datatables and joins them together on the id column of each table. My issue is that sometimes the value that I am joining is in a different column in the datatable and I would like to be able to have the statement as is and to look at the other column as well:

Existing:

Datatable1.AsEnumerable()
           .Join(Datatable2.AsEnumerable(),
               dt1Row => Datatable1.Field<string>(rowid),
               dt2Row=> Datatable2.Field<string>(rowid),
               (dt1Row , dt2Row) => new { dt1Row , dt2Row}).ToList()
           .ForEach(o => {
               o.dt1Row.SetField(o.dt1Row.Table.Columns["name"].Ordinal, o.dt2Row.Field<string>("name1"));

           });

Datatable1.AsEnumerable()
           .Join(Datatable2.AsEnumerable(),
              //Trying to work out?
              ( dt1Row => Datatable1.Field<string>("rowid"),
               dt2Row=> Datatable2.Field<string>("rowid"))
              || ( dt1Row => Datatable1.Field<string>("rowid"),
               dt2Row=> Datatable2.Field<string>("name")),
               (dt1Row , dt2Row) => new { dt1Row , dt2Row}).ToList()

           .ForEach(o => {
               o.dt1Row.SetField(o.dt1Row.Table.Columns["name"].Ordinal, o.dt2Row.Field<string>("name1"));

           });

3 Answers 3

2

sorry for delay, you can use this approach:

var dt1 = DataTable1.AsEnumerable();
var dt2 = DataTable2.AsEnumerable();

 var query = (from dt1Row in dt1
              from dt2Row in dt2
              where dt1Row["rowid"] == dt2Row["rowid"] ||
                    dt1Row["rowid"] == dt2Row["name"]

              select new
              {
                  dt1Row,
                  dt2Row
              });
Sign up to request clarification or add additional context in comments.

Comments

0

you can use iif statement:

.Join(Datatable2.AsEnumerable(),
      dt1Row => Datatable1.Field<string>("rowid"),
      dt2Row => condition ? Datatable2.Field<string>("rowid") : Datatable2.Field<string>("name"),
      (dt1Row , dt2Row) => new { dt1Row , dt2Row})

condition must return a boolean value

5 Comments

can the condition be Datatable1.Field<string>("rowid") == Datatable2.Field<string>("rowid")
there will not syntax error or compilation error with what you wrote, if you can compare them, you can use that instead of condition, but i don't know about logical error, try to check
i think you want to join with second DataTable on its rowid, if its equal to rowid from first DataTable, if not, you want to join first DataTable with second DataTable on rowid from first DataTable with name from second DataTable, if i got right, i think you need to change your linq statements probably
yes thats right, is their a way to do that in that linq ststement
any idea how to change that statement with out having another statement with a join?
0

Added another Linq statement in the Join for Datatable2 using any dt.Any() as the condition

Join(Datatable2.AsEnumerable(),
  dt1Row => Datatable1.Field<string>("rowid"),
  dt2Row => Datatable1.AsEnumerable.Any(x => x.Field<string>("rowid") == Datatable2.Field<string>("rowid"))
? Datatable2.Field<string>("rowid") : Datatable2.Field<string>("name"),
  (dt1Row , dt2Row) => new { dt1Row , dt2Row})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.