티스토리 뷰

WPF

C# LINQ Left join of DataTables

NicSub 2022. 2. 9. 19:19
728x90
반응형

C# LINQ Left join of DataTables

Join 키가 두개일경우

 on new { X1 = tb1.Field("ids"), X2 = tb1.Field("tdate") }
equals new { X1 = tb2.Field("ids"), X2 = tb2.Field("odate") }

 

_____________________________________________________________________________________________________________________


                        var query1 = from tb1 in DT1.AsEnumerable()
                                     join tb2 in DT2.AsEnumerable()
                                           on new { X1 = tb1.Field<string>("ids"), X2 = tb1.Field<string>("tdate") }
                                           equals new
                                           { X1 = tb2.Field<string>("ids"), X2 = tb2.Field<string>("odate") }
                                          into dataKey
                                     from tbResult in dataKey.DefaultIfEmpty()

                                     select new
                                     {
                                         IDS = tb1.Field<string>("ids"),
                                         NAMES = tb1.Field<string>("nm"),
                                         DATES = tb1.Field<string>("dts"),
                                         DNAME = tb1.Field<string>("ynm"),
                                         CHNUM = tb1.Field<string>("cnum"),
                                         T1 = tb1.Field<string>("t1"),
                                         T2 = tb1.Field<string>("t2"),
                                         T3 = tb1.Field<string>("t3"),
                                         T4 = tb1.Field<string>("t4"),
                                         T5 = tb1.Field<string>("t5"),
                                         T6 = tb1.Field<string>("t6"),
                                         T7 = tb1.Field<string>("t7"),
                                         T8 = tb1.Field<string>("t8"),
                                         T9 = tb1.Field<string>("t9"),
                                         T10 = tb1.Field<string>("t10"),

                                         InTime = (tbResult == null ? null : tbResult.Field<string>("tTime")), 
                                         OutTime = (tbResult == null ? null : tbResult.Field<string>("oTime")),
                                   
                                     };

   

// query1 --> joined DataTable

DataTable NDT = new DataTable();
NDT.NewRow();
NDT.Columns.Add("IDS", typeof(string));

~
~

 

   foreach (var r in query1)
      {
         DataRow DR = NDT.NewRow();
         DR["IDS"] = r.IDS;
~
~
        NDT.Rows.Add(DR);

   }

 

 

반응형
댓글