1

Datatable A:

Key1 | Key2 | A | B

[ 1 1 ... ]

[ 1 2 ... ]

Datatable B:

Key1 | Key2 | x | Y

[ 1 1 ... ]

[ 1 2 ... ]

Desired result:

Key1 | Key2 | A | B | X | Y

[ 1 1 ... ]

[ 1 2 ... ]

In the end result columns A, B, X and Y have been added to the new datatable. This happened because key1 and key2 were equal in both datatable A and B. Would this be possible to do with a full outer join, given a condition - (Key1 and Key2 are euqal) ?

7
  • 1
    Can you put the data in your Data tables in a more readable way? Sorry i am still not quite following Commented Apr 14, 2016 at 15:19
  • Maybe a T-SQL example of how you might do this in the database itself will help understand the problem and illuminate the answer. Commented Apr 14, 2016 at 15:25
  • @Shyju I'm on it, haven't found a way to properly format yet. Commented Apr 14, 2016 at 15:25
  • if we could see the data in an actual data table format. one table for A, B and the result. Commented Apr 14, 2016 at 15:31
  • Are you assuming B[....] and Y[...] contain the same values? Commented Apr 14, 2016 at 15:33

2 Answers 2

1
var list1 = (from t1 in dataTable1.AsEnumerable()
                select new
                {
                    Key1 = t1.Field<int>("Key1"),
                    Key2 = t1.Field<int>("Key2"),
                    A = t1.Field<string>("A"),
                    B = t1.Field<string>("B")
                });

var list2 = (from b in dataTable2.AsEnumerable()
                select new
                {
                    Key1 = b.Field<int>("Key1"),
                    Key2 = b.Field<int>("Key2"),
                    X = b.Field<string>("X"),
                    Y = b.Field<string>("Y")
                });

// Now join the 2 collections and get the result you want.

var result = (from x in list1
                join y in list2 on new { x.Key1,x.Key2} equals new { y.Key1,y.Key2 }
                select new { A = x.A, X = y.X }).ToList();

Assuming Key1 and Key2 are int type and A.B,X and Y are of string type.

Sign up to request clarification or add additional context in comments.

4 Comments

Won't AsEnumerable load everything from the database?
From msdn The AsEnumerable<TSource>(IEnumerable<TSource>) method has no effect other than to change the compile-time type of source from a type that implements IEnumerable<T> to IEnumerable<T> itself.
How can I automate this process? Say I only know the 2 datatables have the key1 and key2 columns, but I don't know what other columns they have (I can safely assume they are different columns in the 2 datatables though). I'm asking because here it was done manually (for column X,Y, A and B).
@Shyju Also, what if datatable does exist in memory but is empty? Can what I ask be done?
1

Try this code friend :

var req = (from A in DatatableA
       join B in DatatableB 
        on A.Key1
            equals B.Key1  into DatatableResult
            select new
           {
                  Key1 = A.Key1    ,
                 Key2 = A.Key2    ,
               A= A.A ,
               x= B.x ,
               y= B.y ,

             }).ToList();

1 Comment

Thanks. How can I do this with an empty table? Say table A was empty, and B was the same as in my question. How would I get the same result table as in the question above?

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.