Search code examples
c#joindatatablecross-join

C# cross join datatable


How to obtain cross join for two datatables in C#

dataTable1 - 
          col1 
           a    
           b    

dataTable2 - 
          col2
           c
           d

I want output as follows:

dataResult - col1 col2 
               a    c 
               a    d   
               b    c   
               b    d 

How this can be achieved?


Solution

  • Something like this should do it:

    var results = from dt1 in dataTable1.AsEnumerable()
                  from dt2 in dataTable2.AsEnumerable()
                  select new { dt1, dt2 };