13

I have an Employee class which defined as this:

Employee
{
   public int Id { get; set; }
   public string Name { get; set; }
   public DateTime WorkDate { get; set; }
   public bool isOff { get; set; }
}

This is my class implementation and usage:

List<Employee> workers = new List<Employee>()
{
    new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
    new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
    new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = true},
    new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
    new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
    new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
    new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
    new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
    new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
    new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = false},
    new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
    new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
    new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
    new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
};

In the initialization above there is only 1 record that is off:

Id = 1, Name = "Emp 1" and the WorkDate = 4/13/2016

Now how can i get the id of an employee which has no day off throughout the week (from April 11-17)? Which is Id = 2

A LINQ solution is far better but i dont know how to do it.

UPDATED

To avoid confusion of the object Employee i changed it to EmployeeSchedule

class EmployeeSchedule
{
   public int Id { get; set; }
   public string Name { get; set; }
   public DateTime WorkDate { get; set; }
   public bool isOff { get; set; }
}

This is the implementation

List<EmployeeSchedule> workers = new List<EmployeeSchedule>()
{
    new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = true},
    new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
    new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
};

The selected answer is still applicable.

Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117

5 Answers5

10

Now how can i get the id of an employee which has no day off throughout the week (from April 11-17)? Which is Id = 2

You could use Linq extensions, and achieve this.

var empid = workers.GroupBy(g=> g.Id)           
                   .Where(x=>x.All(e=>!e.IsOff))
                   .Select(x=>x.Key)
Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • 8
    You will need to change the `.Select(x=>x.Id)` to `.Select(x=>x.Key)` as you are working on the grouped elements. – Valeklosse Apr 12 '16 at 06:54
4

That means you need a fromDate, toDate, and you have to find the employees from the list subject to the condition that, WorkDate should be in between fromDate and toDate, and also isOff == false that means has no day off throughout the week So you can use the following snippet :

 DateTime fromDate = new DateTime(2016, 04, 11);
 DateTime toDate = new DateTime(2016, 04, 17);
 var noDayOfList = workers.GroupBy(x=> x.Id)
                          .Where(x =>
                                 x.All(y=> 
                                 y.WorkDate >= fromDate && 
                                 y.WorkDate <= toDate && 
                                 !y.isOff))
                          .Select(z=>z.Key).ToList();   
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
3

You can write something like this:

var startDate = new DateTime(2016,04,11);
var endDate = new DateTime(2016,04,17);

var ids = workers
    .Where(w => w.WorkDate >= startDate)
    .Where(w => w.WorkDate <= endDate)
    .GroupBy(w => w.Id)
    .Where(g => !g.Any(w => w.IsOff))
    .Select(g => g.Key);

Essentially:

  1. Filter the rows you need (in this time frame)
  2. Group by employee id
  3. Make sure none of the rows in the group are marked IsOff
  4. Select the grouping key (in this case, it'll be the Employee's Id)

Note though, it's a bit confusing calling the object Employee, as it really represents an employee's schedule, rather than an employee itself

Rob
  • 26,989
  • 16
  • 82
  • 98
2

try, this you have two condition id and date but your id is sufficient for get your requirement,

List<string> resEmp = new List<string>();

foreach (var item in workers )
 {
     if (item.Id == 2 and item.IsOff == false)
      {
        resEmp.Add(item);
      }
 }
Prabhat Sinha
  • 1,500
  • 20
  • 32
2

Look at these code too.

i have defined a Model class of Employee to get the data,

maybe currently you need Id but in Future you can use another properties too.

public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime WorkDate { get; set; }
        public bool IsOff { get; set; }
    }

and Code wtih Linq Expression,

 Employee objEmployee = new Employee();
 objEmployee.Id = workers
         .GroupBy(itm => itm.Id)
         .Where(itm => !itm.Any(m => m.IsOff))
         .Select(itm => itm.Key)
         .FirstOrDefault();
Bharat
  • 5,869
  • 4
  • 38
  • 58