Left Outer Join on two Datatables in C#.net

问题 Left Outer Join on two Datatables in C#.net

  • 20 Juni 2008 6:39
     
     

    Working on C#

    I have two DataTables with one common column.

    I want to perform a Left outer join on these two and store the result in a third datatable.

     

    Datatable1 contains columns                                                    ColA ColB ColC

    Datatable2 contains columns                                                    Col1 ColA Col2 Col3 Col4 Col5 Col6

     

    Datatable3 will contain                                                              Col A ColB Col 1 Col2   [ColA is common)

     

     

     

    How can I do that?

    An example code would be of great help.

     

    Thanks..

     

Semua Balasan

  • 20 Juni 2008 7:02
     
     

    Pls help Sad

  • 20 Juni 2008 10:26
    Moderator
     
     

    You could use LINQ to create joined result. Here are some samples how to use LINQ with DataTables

     

    http://msdn.microsoft.com/en-us/vbasic/bb738025.aspx

     

    If you cannot use LINQ, you would need to create helper class similar to the following sample

     

    http://support.microsoft.com/kb/326080/en-us

     

  • 25 Juni 2008 13:47
     
     

    I would simply query the database again and populate that DataTable. I like to keep solutions simple. Querying a DataTable is easy enough, but joined tables I haven't found to be easy, if someone has an example I would like to see it.

     

    The example the other gentleman left seems easy enough in LINQ, but I am still on .NET 2.0

     

    So i would just do a left outer join and populate a DataTable, and then query that DataTable again the next time.

     

  • 31 Juli 2012 5:38
     
      Memiliki Kode

    Hi you can check with following Linq query

    var result = from x in dtA.AsEnumerable()
                             join y in dtB.AsEnumerable() on x.Field<string>("AppStatusName").ToUpper() equals y.Field<string>("Application_Stage_fat").ToUpper()
                             into xy
                             from y in xy.DefaultIfEmpty()
                             select new { appstate = x.Field<string>("AppStatusName"), FAT = (y == null) ? null : y.Field<int?>("StatusCount")};
    
    hope this will work


    Hasibul Haque,MCC,MCPD hasibulhaque.com

  • 31 Juli 2012 15:35
     
     

    Just as an FYI Hasibul, check the date when this question was asked. --> Friday, June 20, 2008 6:39 AM

    We probably shouldn't be resurrecting dead threads just to show our astuteness.

    • Diedit oleh JohnGrove 31 Juli 2012 15:36
    •