Below is my table. I want to compare between two dates which have the same employee_id. It's an attendance system. Suppose according to the table, for employee_id =2, in time is 10:00:00 and out time is 17:41:00. Now how can I show in Index view that which one is intime of an employee and which one is outtime of an employee?
Looks like you didn't got my answer, you need to do some changes according to your need always, that was sample code to make it easier for you, here is my complete solution please modify it according to your use, database table etc
//Create a EmployeeAtt class List to show data in VIew
List<EmployeeAtt> empAttList = new List<EmployeeAtt>();
//Create a EmployeeAtt class
EmployeeAtt empatt = new EmployeeAtt();
//Get All Employee
var AllEmp = context.tbl_employee.ToList();
//loop through each emp
foreach (var emp in AllEmp)
{
//Get Array list for each emp using it's Id and sort list according to datetime
var attendnaceList = context.attendance_table.Where(a => a.employee_id == emp.employee_id).OrderBy(a => a.date_and_time).ToArray();
//Get Count
var Count = attendnaceList.Count();
//Loop through all values and increment it by 2 as we will compare two values in/out
for (var i = 0; i < Count; i = i + 2)
{
//create new employee object fo each loop
empatt = new EmployeeAtt();
//Although no need to check but confirm if both the values are of same date,which we will compare
if (attendnaceList[i].date_and_time.Value.Date == attendnaceList[i + 1].date_and_time.Value.Date)
{
//Check which value is less using Time now,as it is sorted first value will be less than second
if (attendnaceList[i].date_and_time.Value.TimeOfDay < attendnaceList[i + 1].date_and_time.Value.TimeOfDay)
{
empatt.Id = Convert.ToInt32(emp.employee_id);
empatt.Emp_name = emp.employee_name;
empatt.Date = attendnaceList[i].date_and_time.Value.Date;
//OUttime Greater one
empatt.OutTime = attendnaceList[i + 1].date_and_time.Value.TimeOfDay;
//In time less value
empatt.InTime = attendnaceList[i].date_and_time.Value.TimeOfDay;
empAttList.Add(empatt);
}
}
}
}
In the above code, I have taken complete employee list then loop through each employee and then getting only it's data and saving it in a List
empAttList
of type EmployeeAtt
, EmployeeAtt
class is as below
public class EmployeeAtt
{
public int Id { get; set; }
public string Emp_name { get; set; }
public DateTime Date { get; set; }
public TimeSpan InTime { get; set; }
public TimeSpan OutTime { get; set; }
}
Now I am rendering IEnumerable<ProjectMVC.Models.EmployeeAtt>
model in view, so here is the output of view
Here are my demo tables, based on your requirement, First table has Employee Id, In/Out time and date
Second Table has Employee Id, Name
You can see in the above example we have "Test Test" employee has 2 different date and different data for IN/OUT time.
You can compare two date and then it's time, suppose you need two compare values of same id
Here is the complete code in C#, I assume here table name is attendance_table & employee_id==2, so your code will to get In and Out time will be
//Get Array list for Employee_id=2 and sort list according to datetime
var attendnaceList = context.attendance_table.Where(a=>a.employee_id==2).OrderBy(a=>a.date_and_time).ToArray();
//Get Count
var Count = attendnaceList.Count();
//Loop through all values and increment it by 2 as we will compare two values in/out
for(var i=0; i< Count; i=i+2)
{
//Although no need to check but confirm if both the values are of same date,which we will compare
if(attendnaceList[i].date_and_time.Value.Date== attendnaceList[i+1].date_and_time.Value.Date)
{
//Check which value is less using Time now,as it is sorted first value will be less than second
if(attendnaceList[i].date_and_time.Value.TimeOfDay < attendnaceList[i + 1].date_and_time.Value.TimeOfDay)
{
//OUttime Greater one
var OutTime = attendnaceList[i + 1].date_and_time.Value.TimeOfDay;
//In time less value
var inTIme = attendnaceList[i].date_and_time.Value.TimeOfDay;
}
}
}
I have explained the code in the comments and it works for me.
This is my Table in the view. I want to show InTime and OutTime written beside every Time_and_date column row.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly