简体   繁体   English

比较C#ASP.NET MVC LINQ实体框架中的两个列表

[英]Comparing two lists in C# ASP.NET MVC LINQ Entity Framework

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). 我有一个flex变量,它将获取一个工作日(8h)这两个日期之间的差值。

"If I stamp in 08:00 and stamp out 17:00, my flex shall be +1(h)" “如果我在08:00戳记并在17:00戳记,则我的弹性为+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. 请帮忙。

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): 现在,您可以将其转换为FlexModel对象(我只填写FlexTime属性,因为不确定如何/为什么需要其他属性):

var workDay = 8;
var flexModel = new FlexModel 
{
    FlexTime = Convert.ToDecimal(flexitime
                   .Sum(f => f.TotalTime - workDay))
};
return View(flexModel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM