Indrajit
Indrajit

Reputation: 51

datatables left join c#

Let T1 and T2 are DataTables with following fields

T1(CustID, ColX, ColY)

T2(CustID, ColZ)

I need to perform Left join on CustID

How this can be done in C# code in a simple way?

Thanks.

Upvotes: 2

Views: 3472

Answers (3)

user1089766
user1089766

Reputation: 627

Use below query for the left outer join

 var query = from t_1 in T1 
                        join t_2 in T2 on t_1.CustID equals t2.CustID into gj
                        from subpet in gj.DefaultIfEmpty()
                        select new { x= t_1.ColX,y=t_2.ColY};

Upvotes: 0

Jeremy Armstrong
Jeremy Armstrong

Reputation: 955

You can use LINQ to do this. Here is the pseudocode for a left join on two data collections:

T1.rows.Select(leftRow=>new{
CustID = leftRow.CustID
ColX = leftRow.ColX
ColY = leftRow.ColY
ColZ = T2.Select(rightRow=>rightRow.ColZ).Where(rightRow=>rightRow.CustID == leftRow.CustID).FirstOrDefault()
});

Upvotes: 2

Erik Dietrich
Erik Dietrich

Reputation: 6090

It seems as though you might be driving at the .NET concept of Linq. If that's the case, this question talks about Left Join using Linq.

If not, just think about what a left join is and create a type that matches what you want (CustID, ColX, ColY, ColZ) where ColZ is nullable and populate it according to rules of left join.

Upvotes: 0

Related Questions