2014-10-03 76 views
1

正如標題所說,即時通訊嘗試比較加蓋日期時間和加蓋日期時間。比較C#中的兩個列表ASP.NET MVC LINQ實體框架

我有一個彈性變量,它應該得到這兩個日期之間的差異值在工作日(8小時)。

「如果我在08:00郵票並加蓋了17:00,我的Flex應+1(H)」

型號:

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; } 
} 

查看:

@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> 

控制器:

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(); 
} 

請幫忙。

+1

顯示衝壓類定義 – 2014-10-03 12:54:54

+0

我剛剛編輯了!請看看... – Qvadriologic 2014-10-03 12:57:54

+0

你的類的實現對我來說看起來不錯如果TimeOut是null,TimeIn'和TimeIn'兩者都會加入'TimeIn'非null是'TimeIn'如果nboth那麼它是' TimeOut' – 2014-10-03 13:00:06

回答

1

可以關聯列表如下:

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

這讓你STAMPIN VS stampout事件列表(雖然戳了可以爲空)。然後,你需要計算這樣的彈性工作時間:

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 
       }; 

現在,您可以將它轉換爲您FlexModel對象(我只填寫FlexTime財產,我不知道如何/爲什麼你需要別人) :

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

很抱歉,但結果不起作用。我需要更具體的詳細說明如何使用這些變量請... – Qvadriologic 2014-10-03 13:53:55

+0

它以什麼方式不起作用? – DavidG 2014-10-03 13:55:24

+0

那麼,視圖中的flex值仍然顯示爲0 ......我不太清楚爲什麼計算結果不起作用! – Qvadriologic 2014-10-03 13:56:20