1

As the title says, im trying to compare the stamped in datetime and stamped out datetime.

I have a flex variable which shall get the value of the difference between those two dates in a workday (8h).

"If I stamp in 08:00 and stamp out 17:00, my flex shall be +1(h)"

Model:

public class FlexModel
{
    public List<User> Users { get; set; }
    public List<Stamping> Stampings { get; set; } 
    public decimal FlexTime { get; set; }
}

public partial class Stamping
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    public int UserId { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    public DateTime Timestamp { get; set; }

    [Required]
    [StringLength(3)]
    public string StampingType { get; set; }

    public virtual User User { get; set; }
}

View:

@Html.LabelFor(model => model.FlexTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
    @Html.DisplayFor(model => model.FlexTime, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.FlexTime, "", new { @class = "text-danger" })
</div>

Controller:

public ActionResult Info()
{
    var flexModel = new FlexModel();
    var userId = (int)Session["userId"];
    var user = _db.Users.Find(userId);
    var stampIn = _db.Stampings
                     .Where(i => i.StampingType == "in")
                     .Where(i => i.User == user)
                     .ToList();

    var stampOut = _db.Stampings
                      .Where(i => i.StampingType == "out")
                      .Where(i => i.User == user)
                      .ToList();

    var workDay = 8;

    if (stampIn.Count == 0)
    {
        return View();
    }

    foreach (var itemIn in stampIn)
    {
        //Dont know what to do here
    }

    foreach (var itemOut in stampOut)
    {
        //Dont know what to do here either
    }

    return View();
}

Please help.

6
  • 1
    show Stamping class definition Commented Oct 3, 2014 at 12:54
  • I just edited it! Have a look please... Commented Oct 3, 2014 at 12:57
  • your class implementation not looks fine to me add in the class TimeOut and TimeIn both if TimeOut is null and TimeIn not null is TimeIn if nboth then it is TimeOut Commented Oct 3, 2014 at 13:00
  • That doesnt really help me do the calculation sir... I need to know how to compare these two lists to each other, through the hours of it. Commented Oct 3, 2014 at 13:01
  • in both list how will you relate timein and timout of user think about it Commented Oct 3, 2014 at 13:02

1 Answer 1

1

You can correlate the lists like this:

var attendance = from sin in stampIn
                 select new 
                 {
                    StampIn = sin,
                    StampOut = stampOut.FirstOrDefault(sout => 
                                    sout.Timestamp > sin.Timestamp)
                 };

That gives you a list of stampin vs stampout events (though the stamp out may be null). Then you need to calculate the flexitime like this:

var flexitime = from att in attendance
                select new
                {
                    TimeIn = att.StampIn.Timestamp,
                    TimeOut = att.StampOut == null ? (DateTime?)null : att.StampOut.Timestamp,
                    TotalTime = att.StampOut == null ? 0 :  
                        att.StampOut.Timestamp.Subtract(att.StampIn.Timestamp).TotalHours
                };

You can now convert this to your FlexModel object (I'll only fill in the FlexTime property as I'm not sure how/why you need the others):

var workDay = 8;
var flexModel = new FlexModel 
{
    FlexTime = Convert.ToDecimal(flexitime
                   .Sum(f => f.TotalTime - workDay))
};
return View(flexModel);
Sign up to request clarification or add additional context in comments.

4 Comments

Im sorry, but the result does not work. I need more specific detail of how to use these variables please...
Well, the flex value still shows 0 in the View... Im not quite sure why the calculate wont work!
FlexTime = flexitime.Sum(f => f.TotalTime) - "Cannot convert source type 'double' to target 'decimal'
Yes Ok, it works. But the problem now is that it adds up no matter what time the user stamp in or out. It has to be more than 8 hours to add, only then should it use the difference. And ofcourse vice versa if it's less than 8 hours for that day...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.