Jogi
Jogi

Reputation: 1

Join Tables in LINQ

I have a table with following format:

---------------------------------------------
|name |  date  | timeTable |clockIn|clockOut|
---------------------------------------------
|jhon |01/02/15| Mornning  | 08:29 |______  |
---------------------------------------------
|jhon |01/02/15| Afternoon |_______|  04:31 |
---------------------------------------------
|Harry|01/02/15| Mornning  | 08:23 |_______ |
---------------------------------------------
|Harry|01/02/15| Afternoon |_______|  04:29 |
---------------------------------------------

From this above table, my desired format is:

---------------------------------------------
|name |  date  | clockIn|clockOut|
---------------------------------------------
|jhon |01/02/15|  08:29 |  04:31 |
---------------------------------------------
|Harry|01/02/15|  08:23 |  04:29 |
---------------------------------------------

My working is as follows: Controller

 var t1 = (from a in hc.attendanceLogs
                  where (a.timeTable == "Morning")
                  select new
                  {
                      name = a.name,
                      date = a.date,
                      timeTable = a.timeTable,
                      clockIn = a.clockIn
                  }).ToList();

    var t2 = (from a in hc.attendanceLogs
              where a.timeTable == "Afternoon"
              select new
              {
                  date = a.date,
                  clockOut = a.clockOut
              }).ToList();

    DataTable finalTable = new DataTable();
                        finalTable.Columns.Add("name", typeof(string));
                        finalTable.Columns.Add("date", typeof(string));
                        finalTable.Columns.Add("clockIn", typeof(string));
                        finalTable.Columns.Add("clockOut", typeof(string));

                        var t3 = from a in t1
                                 join d in t2
                                 on
                                 a.date equals d.date
                                 select
                                 finalTable.LoadDataRow(
                                     new object[]
                                 { a.name, a.date, a.clockIn, d.clockOut },
                                     false);
    ViewBag.Data = finalTable; 

What I'm trying to do is, cut first 4 columns of the initial table hc.attendanceLogs and popluate them in variable t1. Then I take the last column of the initial table hc.attendanceLogs and popluate it in variable t2. Finally, join t1 with t2 into finalTable, where date in t1 is equal to date in t2.

Problem is ViewBag.Data is coming as empty when I try to display finalTable in a View. Any ideas where I'm going wrong?

Upvotes: 0

Views: 48

Answers (1)

sgmoore
sgmoore

Reputation: 16067

I think this is because you are not actually evaluating or iterating through t3.

Try adding a .ToList() and the end of the var t3 == ... line.

Upvotes: 1

Related Questions