Nate
Nate

Reputation: 413

DataGrid Duplicate Data which should not occur

So I am currently creating an AttendanceApp using C# WPF. I have a DataGrid that displays and calculate attendance metrics automatically from two files which is from the attendance file and the actual attendance logs.

I have tried matching the two files by a UserID which works, but when I start to deal with more than 1 day. I get duplicate and wrong data.

At the moment I have only 2 days worth of data for 5 users but User #1 has 8 lists already when it should only have 2 for 2 days worth of attendance

Duplicate Entries DataGrid View1

But when the data is only enough for 1 day the data looks correct

One Day worth of Attendance DataGrid View2

So I am having duplicate and erroneous data at my DataGrid

Here are my sources of data

Employee Schedule

Employee Schedule

Employee Attendance Logs

Employee Attendance Logs

And this is my code that I am using to populate the DataGrid

public void Main()
        {
            List<Emp1> emps;
            List<Actual> actuals;

            actuals = emp.GetActual(@"C:\Users\IT\Desktop\Sample\SampleActual.dat");
            emps = GetEmpSched();



            var final = (from a1 in actuals
                         join a2 in actuals on a1.ID equals a2.ID
                         join t1 in emps on a1.ID equals t1.ID
                         join t2 in emps on a2.ID equals t2.ID
                         where (a1.LogStatus == 0) && (a2.LogStatus == 1)
                         select new
                         {
                             User_ID = t1.ID,
                             Scheduled_In = t1.In,
                             Actual_Login = a1.ActualLog,
                             Scheduled_Out = t2.Out,
                             Actual_Out = a2.ActualLog
                         }).Distinct(). ToList();

            tbContainer = StaticClasses.ToDataTable(final);

            dgvAttendance.ItemsSource = emp.CalculateEmployeeAttendance(tbContainer);
        }

As you see I have added the Distinct() to try to filter out if it works.

The attendance logs works like if its zero its a time in, and if its a one its a time out.

so I declared emps twice to filter so that all LogStatus == 0 will be for Actual Time ins, and Logstatus == 1 will be for Actual Time outs.

Any ideas would be helpful.

Upvotes: 2

Views: 163

Answers (1)

Imran Hossain
Imran Hossain

Reputation: 91

The duplication is happening because you are joining the tables based on only the id of the users. Your code has no way of differentiating them based on the date. That's why the schedule table of your first day is getting joined with both the actual times of your first day and second day's data. Tomorrow, when you have another set of actuals, you are gonna have each row repeating itself three times.

To fix it, add another field in both tables which contains the date (date, month, year), then when joining, consider that field too

Upvotes: 1

Related Questions