How to Left Outer Join two DataTables in c#?

后端 未结 3 1701
青春惊慌失措
青春惊慌失措 2020-12-09 09:46

How can I Left Outer Join (I think it is Left Outer Join but I am not 100% sure) two data tables with the following tables and conditions while keeping all columns from both

3条回答
  •  被撕碎了的回忆
    2020-12-09 10:11

    Thanks all for your help. Here is what I came up with based on multiple resources:

    public static class DataTableHelper
    {
        public enum JoinType
        {
            /// 
            /// Same as regular join. Inner join produces only the set of records that match in both Table A and Table B.
            /// 
            Inner = 0,
            /// 
            /// Same as Left Outer join. Left outer join produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no match, the right side will contain null.
            /// 
            Left = 1
        }
    
        /// 
        /// Joins the passed in DataTables on the colToJoinOn.
        /// Returns an appropriate DataTable with zero rows if the colToJoinOn does not exist in both tables.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// http://stackoverflow.com/questions/2379747/create-combined-datatable-from-two-datatables-joined-with-linq-c-sharp?rq=1
        /// http://msdn.microsoft.com/en-us/library/vstudio/bb397895.aspx
        /// http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
        /// http://stackoverflow.com/questions/406294/left-join-and-left-outer-join-in-sql-server
        /// 
        public static DataTable JoinTwoDataTablesOnOneColumn(DataTable dtblLeft, DataTable dtblRight, string colToJoinOn, JoinType joinType)
        {
            //Change column name to a temp name so the LINQ for getting row data will work properly.
            string strTempColName = colToJoinOn + "_2";
            if (dtblRight.Columns.Contains(colToJoinOn))
                dtblRight.Columns[colToJoinOn].ColumnName = strTempColName;
    
            //Get columns from dtblLeft
            DataTable dtblResult = dtblLeft.Clone();
    
            //Get columns from dtblRight
            var dt2Columns = dtblRight.Columns.OfType().Select(dc => new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping));
    
            //Get columns from dtblRight that are not in dtblLeft
            var dt2FinalColumns = from dc in dt2Columns.AsEnumerable()
                                  where !dtblResult.Columns.Contains(dc.ColumnName)
                                  select dc;
    
            //Add the rest of the columns to dtblResult
            dtblResult.Columns.AddRange(dt2FinalColumns.ToArray());
    
            //No reason to continue if the colToJoinOn does not exist in both DataTables.
            if (!dtblLeft.Columns.Contains(colToJoinOn) || (!dtblRight.Columns.Contains(colToJoinOn) && !dtblRight.Columns.Contains(strTempColName)))
            {
                if (!dtblResult.Columns.Contains(colToJoinOn))
                    dtblResult.Columns.Add(colToJoinOn);
                return dtblResult;
            }
    
            switch (joinType)
            {
    
                default:
                case JoinType.Inner:
                    #region Inner
                    //get row data
                    //To use the DataTable.AsEnumerable() extension method you need to add a reference to the System.Data.DataSetExtension assembly in your project. 
                    var rowDataLeftInner = from rowLeft in dtblLeft.AsEnumerable()
                                           join rowRight in dtblRight.AsEnumerable() on rowLeft[colToJoinOn] equals rowRight[strTempColName]
                                           select rowLeft.ItemArray.Concat(rowRight.ItemArray).ToArray();
    
    
                    //Add row data to dtblResult
                    foreach (object[] values in rowDataLeftInner)
                        dtblResult.Rows.Add(values);
    
                    #endregion
                    break;
                case JoinType.Left:
                    #region Left
                    var rowDataLeftOuter = from rowLeft in dtblLeft.AsEnumerable()
                                           join rowRight in dtblRight.AsEnumerable() on rowLeft[colToJoinOn] equals rowRight[strTempColName] into gj
                                           from subRight in gj.DefaultIfEmpty()
                                           select rowLeft.ItemArray.Concat((subRight== null) ? (dtblRight.NewRow().ItemArray) :subRight.ItemArray).ToArray();
    
    
                    //Add row data to dtblResult
                    foreach (object[] values in rowDataLeftOuter)
                        dtblResult.Rows.Add(values);
    
                    #endregion
                    break;
            }
    
            //Change column name back to original
            dtblRight.Columns[strTempColName].ColumnName = colToJoinOn;
    
            //Remove extra column from result
            dtblResult.Columns.Remove(strTempColName);
    
            return dtblResult;
        }
    }
    

    EDIT 3:

    This method now works correctly and it is still fast when the tables have 2000+ rows. Any recommendations/suggestions/improvements would be appreciated.

    EDIT 4:

    I had a certain scenario that led me to realize the previous version was really doing an inner join. The function has been modified to fix that problem. I used info at this link to figure it out.

提交回复
热议问题