简体   繁体   中英

Full outer join two lists of separate classes into a third list of a third class

I am trying to full outer join two lists (of separate classes), the reason I have to do it this way is since I need all the orders from my system, along with orders from another system. Some orders match on ordernumber and some doesn't.But I still want to display all the orders in a grid.

The two interfaces look like this

     interface IOrderInternal
     {
          int ID { get; } // My systems inernal ID
          string NO_Order { get; } // My external ID
          DateTime? OrderDate { get; }
     }

     interface IOrderExternal
     {
          int ID { get; } // External systems internal ID
          string ProjectNumber{ get; }
     }

The join of hese two interfaces should end up being

     interface IOrderInternal
     {
          int ID_IN { get; set; }
          int ID_EX { get; set; }
          string ProjectNumber{ get; set; }
          DateTime OrderDate { get; set; }
     }

So two lists are passed in in a function that should full outer join them, based on NO_Order = ProjectNumber

     IList<IOrderInternal> INOrders
     IList<IOrderExternal> EXOrders

     var leftJoin = from exorder in EXOrders
                    join inorder in INOrders
                        on exorder.ProjectNumber equals inorder.NO_Order
                    into temp 
                    from inorder in temp
                    select new Order
                    {
                        ID_IN = inorder == null ? default(int) : inorder.ID,
                        ID_EX = exorder.ID,
                        ProjectNumber = inorder == null ? default(string) : inorder.NO_Order
                        OrderDate = inorder == null ? default(DateTime) : inorder.OrderDate
                    };

     var rightJoin = from inorderin INOrders
                    join exorderin EXOrders
                        on inorder.NO_Order equals exorder.ProjectNumber
                    into temp 
                    from exorder in temp
                    select new Order
                    {
                        ID_IN = inorder.ID,
                        ID_EX = exorder == null? default(int) : exorder.ID,
                        ProjectNumber = inorder.NO_Order
                        OrderDate = inorder.OrderDate
                    };

     var joinedList = leftJoin.Union(rightJoin);

When I join the lists I only get the results of an inner join. But I need to know all the orders in total.

Any help is appreciated :)

If I understand you correctly, you want to do outer joins in either direction and then concatenate the results together? This is how I do left joins in linq:

 IList<IOrderInternal> INOrders
 IList<IOrderExternal> EXOrders

 var leftJoin = from exorder in EXOrders
                from inorder in INOrders.Where(x => exorder.ProjectNumber == x.NO_Order).DefaultIfEmpty()
                select new Order
                {
                    ID_IN = inorder == null ? default(int) : inorder.ID,
                    ID_EX = exorder.ID,
                    ProjectNumber = inorder == null ? default(string) : inorder.NO_Order
                    OrderDate = inorder == null ? default(DateTime) : inorder.OrderDate
                };

 var rightJoin = from inorderin INOrders
                from exorderin EXOrders.Where(x => inorder.NO_Order == x.ProjectNumber).DefaultIfEmpty()
                select new Order
                {
                    ID_IN = inorder.ID,
                    ID_EX = exorder == null? default(int) : exorder.ID,
                    ProjectNumber = inorder.NO_Order
                    OrderDate = inorder.OrderDate
                };

 var joinedList = leftJoin.Union(rightJoin);

I like this way because it keeps the left join all on one line, but if you wanted to continue using your syntax, I think you would only have to change temp to temp.DefaultIfEmpty() in each query to get it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM