简体   繁体   中英

How to do full outer join 2 data tables

DataTable1

LoginId LoginName SCount
1       Mohit     20
3       Riya      25

DataTable2

LoginId  LoginName ECount
2        Smita     11
3        Riya      13   

I want to show result like this

LoginName  Scount  Ecount Total
Mohit      20      0      20
Smita      0       11     11
Riya       25      13     38

in sql I am doing like this

select LoginId,LoginName,Scount,Ecount,(Scount+Ecount) as Total
  from
  (
      select
      CASE WHEN t1.LoginId is null THEN  t2.LoginId ELSE t1.LoginId END as LoginId,
      CASE WHEN t1.LoginName is null THEN  t2.LoginName ELSE t1.LoginName END as LoginName,
      CASE WHEN t1.SCount is null THEN  0 ELSE t1.SCount END as Scount,
      CASE WHEN t2.ECount is null THEN  0 ELSE t2.ECount END as Ecount
      FROM [table2] t2
      full outer join [table1] t1
      on t1.LoginId = t2.LoginId
  )
  A
  order by LoginId

How to do in C#

I tried like this

DataTable dtResult = new DataTable();

DataTable UserCount1 = new DataTable();
DataTable UserCount2 = new DataTable();

// Assigning value to datatable 

if (ds != null)
{
     UserCount1 = ds.Tables["UserCount1"];
     UserCount2 = ds.Tables["UserCount2"];
}

var LinqResult = 
     from dataRows1 in UserCount1.AsEnumerable()
     join dataRows2 in UserCount2.AsEnumerable()
     on dataRows1.Field<string>("LoginId") equals dataRows2.Field<string>("LoginId") into lj
     from r in lj.DefaultIfEmpty()
     select dtResult.LoadDataRow(new object[]
     {
         dataRows2.Field<string>("LoginName"),
         r == null ? 0 : r.Field<int>("SCount"),
         r == null ? 0 : r.Field<int>("ECount")
     }, false);

How to do full outer join in C#.I am new to linq.How to identify whether it is right or left .

You can use the Union for the left and the right join :

try the code snippet :

var leftJoin = from dataRows1 in UserCount1.AsEnumerable()
              join dataRows2 in UserCount2.AsEnumerable()
              on dataRows1.Field<string>("LoginId") equals dataRows2.Field<string>("LoginId") into lj
              from r in lj.DefaultIfEmpty()
              select new
              {
                 LoginName = dataRows1.Field<string>("LoginName"),
                 SCount = dataRows1.Field<int>("SCount"),
                 ECount = r?.Field<int>("ECount") ?? 0,
                 Total = dataRows1.Field<int>("SCount") + (r?.Field<int>("ECount") ?? 0)
              };

var rightJoin = from dataRows2 in UserCount2.AsEnumerable()
                join dataRows1 in UserCount1.AsEnumerable()
                on dataRows2.Field<string>("LoginId") equals dataRows1.Field<string>("LoginId") into rj
                from l in rj.DefaultIfEmpty()
                select new
                {
                   LoginName = dataRows2.Field<string>("LoginName"),
                   SCount = l?.Field<int>("SCount") ?? 0,
                   ECount = dataRows2.Field<int>("ECount"),
                   Total = dataRows2.Field<int>("ECount") + (l?.Field<int>("SCount") ?? 0)
                };

var result = leftJoin.Union(rightJoin);

i hope that will help you out

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