2017-11-16 14 views
-1

どのようにデータを下のクラスリストに追加しますか?両方のリストは動的であり、dailyingsclockingsリストはクロッキングリストの各項目ごとに異なる場合があります。 私は現在、各リスト、クッキー、およびdailyclockingsにデータを追加することができますが、dailyclockingsのデータはclockings [1] .dailyclockings [1]に引き込まれません。私はclockings [1] .nameとdailyclockings [1] .clockingdateのデータしか見ることができませんが、clockings [1] .dailyclocking [1] .clockingdateを表示すると値はnullです。私は何を達成しようとしてることは、あなたのコードには大きな誤解があり、次のC#リストと非リスト項目のクラス

Clockings[1].Department = "Testing" 
Clockings[1].Name = "Fred" 
Clockings[1].DailyClockings[1].ClockingDate = "2017/11/01" 
Clockings[1].DailyClockings[1].InClockingTime = "08:00" 
Clockings[1].DailyClockings[1].OutClockingTime = "14:00" 
Clockings[1].DailyClockings[2].ClockingDate = "2017/11/02"  
Clockings[1].DailyClockings[2].InClockingTime = "08:00" 
Clockings[1].DailyClockings[2].OutClockingTime = "14:00" 
Clockings[1].DailyClockings[3].ClockingDate = "2017/11/03"  
Clockings[1].DailyClockings[3].InClockingTime = "08:00" 
Clockings[1].DailyClockings[3].OutClockingTime = "14:00" 
Clockings[1].DailyClockings[4].ClockingDate = "2017/11/06"  
Clockings[1].DailyClockings[4].InClockingTime = "08:00" 
Clockings[1].DailyClockings[4].OutClockingTime = "14:00" 



    public class Clockings 
    { 

     public string Department { get; set; } 
     public string UserName { get; set; } 
     public string StartTime { get; set; } 
     public int NoofDaysClocked { get; set; } 

     public List<DailyClocking> DailyClockings { get; set; } 


    } 

    public class DailyClocking 
    { 
     public string InClockingTime { get; set; } 
     public string OutClockingTime { get; set; } 
     public string ClockingDate { get; set; } 
     public string DayofTheWeek { get; set; } 
     public string MinInEarly { get; set; } 
     public string MinOutEarly { get; set; } 
    } 

public List<Clockings> clockingTime() 
     { 
      List<Clockings> f = new List<Clockings>() ; 

      Clockings d = new Clockings(); 

      DailyClocking d1 = new DailyClocking(); 
      List<DailyClocking> d2 = new List<DailyClocking>(); 

      float noofusers =1; 
      int col = 0; 
      int tmpemp = 0; 
      int n =0, x = 0; 


      var cmd = db.Database.Connection.CreateCommand(); 

      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.CommandText = "[dbo].LateClocking"; 

      SqlParameter param = new SqlParameter(); 
      param.ParameterName = "@StartDate"; 
      param.Value = 20170901; 
      cmd.Parameters.Add(param); 

      SqlParameter param1 = new SqlParameter(); 
      param1.ParameterName = "@EndDate"; 
      param1.Value = 20170930; 
      cmd.Parameters.Add(param1); 

      SqlParameter param2 = new SqlParameter(); 
      param2.ParameterName = "@Branch"; 
      param2.Value = ""; 
      cmd.Parameters.Add(param2); 

      SqlParameter param3 = new SqlParameter(); 
      param3.ParameterName = "@department"; 
      param3.Value = ""; 
      cmd.Parameters.Add(param3); 

      SqlParameter param4 = new SqlParameter(); 
      param4.ParameterName = "@Nme"; 
      param4.Value = "Fred Smith"; 
      cmd.Parameters.Add(param4); 




      try 
      { 
       cmd.Connection.Open(); 
       var reader = cmd.ExecuteReader(); 


       if (reader.HasRows) 
       { 

        while (reader.Read()) 
        { 
         if (tmpemp == int.Parse(reader["mst_sq"].ToString())) 
         {} 
         else 
         { 
          if (noofusers >1) 
          { 
           f.Add(d); 
          } 
          col = 0; 
          noofusers = noofusers + 1; 
          tmpemp = int.Parse(reader["mst_sq"].ToString()); 
          d.UserName = reader["mst_firstname"].ToString() + " " + reader["mst_lastname"].ToString(); 
          d.StartTime = reader["starttime"].ToString(); 
          d.Department = reader["dept_name"].ToString(); 
         } 
         if (reader["dy"].Equals(System.DBNull.Value)) 
         {} 
         else 
         { 
          d1.ClockingDate = reader["dy"].ToString().Substring(0, 4) + reader["dy"].ToString().Substring(5, 2) + reader["dy"].ToString().Substring(8, 2); 
          d1.MinInEarly = reader["mnearly"].ToString(); 
          d1.MinInEarly = reader["mnlate"].ToString(); 
          d1.InClockingTime = reader["tmeclocked"].ToString().Substring(11, 5); 
          d1.OutClockingTime = reader["earlyclocked"].ToString().Substring(11, 5); 
          d2.Add(d1); 
         } 
         col = col + 1; 
         d.NoofDaysClocked = col; 





        } 
        f.Add(new f.DailyClockings(d2)); 
        f.Add(d); 
       } 

      } 
      catch (Exception ex) 
      { 
       // your handling code here 

      } 

      return (f); 
     } 

答えて

0

は、すみません、私はコメントだろうが、私の評判は私を制限します。

とにかく、ここでこの部分に:あなたはClockings listを取る場合、オブジェクトとしてリストにDailyClocking listを追加し、その後リストにClockingsオブジェクトを追加するよう

f.Add(new f.DailyClockings(d2)); 
f.Add(d); 

に見えます。

これはおそらくこのようなものでしょうか? :

d.DailyClockings = d2; 
f.Add(d); 
+0

これは私が私の問題を解決する助けていただきありがとうございます。今私は2番目の問題を抱えています。 d2のとき。add(d1)が実行されて新しい項目が追加され、上のすべての要素が新しい値で書き込まれます。これらの質問はC#を初めてお手伝いして申し訳ありません。私はVBコーダーであり、VB.netでも同様のことをしています。 –

+0

前のコードを使って、 'd2.Add(DailyClocking(d1));' を試すことをお勧めします。これらの参照から新しいオブジェクトを作成するのではなく、毎回同じ参照を使用している可能性があります。 – Jack

0

ある

  if (reader.HasRows) 
      { 

       while (reader.Read()) 
       { 
        if (tmpemp == int.Parse(reader["mst_sq"].ToString())) 
        {} 
        else 
        { 
         if (noofusers >1) 
         { 
          f.Add(d); 
         } 
         col = 0; 
         noofusers = noofusers + 1; 
         tmpemp = int.Parse(reader["mst_sq"].ToString()); 
         d.UserName = reader["mst_firstname"].ToString() + " " + reader["mst_lastname"].ToString(); 
         d.StartTime = reader["starttime"].ToString(); 
         d.Department = reader["dept_name"].ToString(); 
        } 
        if (reader["dy"].Equals(System.DBNull.Value)) 
        {} 
        else 
        { 
         d1.ClockingDate = reader["dy"].ToString().Substring(0, 4) + reader["dy"].ToString().Substring(5, 2) + reader["dy"].ToString().Substring(8, 2); 
         d1.MinInEarly = reader["mnearly"].ToString(); 
         d1.MinInEarly = reader["mnlate"].ToString(); 
         d1.InClockingTime = reader["tmeclocked"].ToString().Substring(11, 5); 
         d1.OutClockingTime = reader["earlyclocked"].ToString().Substring(11, 5); 
         d2.Add(d1); 
        } 
        col = col + 1; 
        d.NoofDaysClocked = col; 



       } 
       f.Add(new f.DailyClockings(d2)); 
       f.Add(d); 

1)はログ、例外処理を追加してください。

if (tmpemp == int.Parse(reader["mst_sq"].ToString())) 
        {} 

かで:

 if (reader["dy"].Equals(System.DBNull.Value)) 
      {} 

あり、エラーまたはデシベルのnullの場合もありますので、あなたのリストの一つが人口取得することはありません、あなたはそれについて知ることができません、これで はnull値になります。

2)あなたのリスト内のオブジェクトを初期化しないでください:

f.Add(new f.DailyClockings(d2)); 

あなたは、新しいオブジェクトをインスタンス化する過程で、あなたのデータを失う可能性があります。

はこのようにそれを使用してください:私が間違っている場合

f.DailyClockings dailyClockingsList = new List<DailyClockings> 

    dailyClockingsList.Add(d2); 
        f.Add(d); 
0

最後に、私は上記の答えの一部を使って私の問題を解決しました。以下は、そのソリューションが最良の方法であるかどうかを確認するために使用されていますが、動作し、私に必要な結果を提供します。

私のコントローラで以下のコードを表示し、site.cssファイルに変更を加えてテーブルのフォーマットを可能にしました。以下は、ユーザーがストアドプロシージャに渡すパラメータを選択するビューから呼び出されます。

private List<Clockings> clockingTime(string Branch, DateTime StartDate, DateTime Enddate, string Name, String Department, string ReportType) 
    { 
     List<Clockings> f = new List<Clockings>(); 
     List<DailyClocking> d2 = new List<DailyClocking>(); 
     string UserName = "", StartTime = "", BranchFromSP = "", DepartmentFromSP = ""; 
     int NoofDaysClocked = 0; 

     float noofusers = 1; 
     int tmpemp = 0; 
     int col = 0; 

     var cmd = db.Database.Connection.CreateCommand(); 

     cmd.CommandType = CommandType.StoredProcedure; 
     if (ReportType == "ALLDOORS") 
     { 

     } 
     else 
     { 
      cmd.CommandText = "[dbo].LateClocking"; 
     } 
     SqlParameter param = new SqlParameter(); 
     param.ParameterName = "@StartDate"; 
     param.Value = StartDate.ToString("yyyyMMdd"); 
     cmd.Parameters.Add(param); 

     SqlParameter param1 = new SqlParameter(); 
     param1.ParameterName = "@EndDate"; 
     param1.Value = Enddate.ToString("yyyyMMdd"); 
     cmd.Parameters.Add(param1); 

     SqlParameter param2 = new SqlParameter(); 
     param2.ParameterName = "@Branch"; 
     param2.Value = Branch; 
     cmd.Parameters.Add(param2); 
     if (Department.ToUpper() == "NONE") 
     { 
      Department = ""; 
     } 
     SqlParameter param3 = new SqlParameter(); 
     param3.ParameterName = "@department"; 
     param3.Value = Department; 
     cmd.Parameters.Add(param3); 
     if (Name.ToUpper() == "ALL") 
     { 
      Name = ""; 
     } 
     SqlParameter param4 = new SqlParameter(); 
     param4.ParameterName = "@Nme"; 
     param4.Value = Name; 
     cmd.Parameters.Add(param4); 


     try 
     { 
      cmd.Connection.Open(); 
      var reader = cmd.ExecuteReader(); 


      if (reader.HasRows) 
      { 

       while (reader.Read()) 
       { 
        if (tmpemp == int.Parse(reader["mst_sq"].ToString())) 
        { } 
        else 
        { 
         if (noofusers > 1) 
         { 


          f.Add(new Clockings(
           BranchFromSP, 
           DepartmentFromSP, 
           UserName, 
           StartTime, 
           NoofDaysClocked, 
           d2 
           )); 
         } 
         col = 0; 
         noofusers = noofusers + 1; 
         tmpemp = int.Parse(reader["mst_sq"].ToString()); 
         UserName = reader["mst_firstname"].ToString() + " " + reader["mst_lastname"].ToString(); 
         StartTime = reader["starttime"].ToString(); 
         DepartmentFromSP = reader["dept_name"].ToString(); 
         BranchFromSP = reader["branch"].ToString(); 

         d2.Clear(); 
        } 
        if (reader["dy"].Equals(System.DBNull.Value)) 
        { } 
        else 
        { 
         d2.Add(new DailyClocking 
         (
          reader["dy"].ToString().Substring(0, 4) + reader["dy"].ToString().Substring(5, 2) + reader["dy"].ToString().Substring(8, 2), 
          reader["dy"].ToString(), 
          reader["mnearly"].ToString(), 
          reader["mnlate"].ToString(), 
          reader["tmeclocked"].ToString().Substring(11, 5), 
          reader["earlyclocked"].ToString().Substring(11, 5) 
         )); 
        } 
        col = col + 1; 
        NoofDaysClocked = col; 


       } 

       f.Add(new Clockings(
        BranchFromSP, 
           DepartmentFromSP, 
        UserName, 
        StartTime, 
        NoofDaysClocked, 
        d2 
        )); 
      } 

     } 
     catch (Exception ex) 
     { 
      // your handling code here 
      ex = ex; 
     } 


     return (f); 



<html> 
<head> 

<meta name="viewport" content="width=device-width" /> 


<title>Clocking Report</title> 
</head> 
<body> 
<span style="font-size:small;float:right">@Html.ActionLink("Back", "ClockingParamaters", "ManagerSection")</span> 
<div style="width: 100%; height: 500px; overflow: auto;"> 
    <table class="table" ; style="width:@(MostDaysClock+"px");"> 
     <thead> 
      <tr> 
       <th style="width:70px; height:100px;padding-top:80px;"> 
        Branch 
       </th> 
       <th style="width:150px; height:100px; padding-top:80px;"> 
        Department 
       </th> 
       <th style="width:175px; height:100px; padding-top:80px;"> 
        Name 
       </th> 
       <th style="width:100px; height:100px; padding-top:80px;"> 
        Start Time 
       </th> 
       @{ 

        DateTime StartDate = ViewBag.StartDate; 
        DateTime EndDate = ViewBag.EndDate; 

        for (DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1)) 
        { 
         ColumnHeader[ColumnCount] = date.ToString("yyyyMMdd"); 
         ColumnCount++; 
         <th class="rotate" ; style="width: 100px;"> 
          <div> 
           <span> 

            @Html.DisplayFormattedData(date.ToString("yyyy/MM/dd")) 

           </span> 
          </div> 
         </th> 


        } 
        TotalColumnCount = ColumnCount; 
       } 
      </tr> 

     <tbody style="height: 100%; "> 

      @foreach (var ClockingDisplay in Model) 
      { 
       ColumnCount = 0; 
       StaffClockingCount = 0; 
       <tr style="height:40px;"> 
        <td style="width:70px;"> @Html.DisplayFor(modelItem => ClockingDisplay.Branch)</td> 
        <td style="width:150px;white-space:nowrap; text-overflow: ellipsis;overflow:hidden"> @Html.DisplayFor(modelItem => ClockingDisplay.Department)</td> 
        <td style="width:175px;"> @Html.DisplayFor(modelItem => ClockingDisplay.UserName)</td> 
        <td style="width:125px;"> @Html.DisplayFor(modelItem => ClockingDisplay.StartTime)</td> 
        @do 
        { 
         if (ClockingDisplay.DailyClockings.Count > 0) 
         { 


          if (ClockingDisplay.DailyClockings[StaffClockingCount].ClockingDate == ColumnHeader[ColumnCount]) 
          { 

           if (Convert.ToInt32(ClockingDisplay.DailyClockings[StaffClockingCount].MinOutEarly) < 0) 
           { 
            Double vl = Convert.ToInt32(ClockingDisplay.DailyClockings[StaffClockingCount].MinOutEarly) * -1; 
           <td style="width:50px;height:40px;border:solid;border-width:1px;text-align:center; background-color:red;font-size:small ">@Html.DisplayFormattedData(vl.ToString())</td> 
           } 
           else if (Convert.ToInt32(ClockingDisplay.DailyClockings[StaffClockingCount].MinOutEarly) == 0) 
           { 
           <td style="width:50px;height:40px;border:solid;border-width:1px;text-align:center; font-size:small ">@Html.DisplayFormattedData(ClockingDisplay.DailyClockings[StaffClockingCount].MinOutEarly)</td> 
           } 
           else 
           { 
           <td style="width:50px;height:40px;border:solid;border-width:1px;text-align:center; background-color:green;font-size:small ">@Html.DisplayFormattedData(ClockingDisplay.DailyClockings[StaffClockingCount].MinOutEarly)</td> 
           } 

           if (Convert.ToInt32(ClockingDisplay.DailyClockings[StaffClockingCount].MinInEarly) < 0) 
           { 
            Double vl = Convert.ToInt32(ClockingDisplay.DailyClockings[StaffClockingCount].MinInEarly) * -1; 

           <td style="width:50px;height:40px;border:solid;border-width:1px;text-align:center;background-color:green; font-size:small ">@Html.DisplayFormattedData(vl.ToString())</td> 
           } 
           else if (Convert.ToDouble(ClockingDisplay.DailyClockings[StaffClockingCount].MinInEarly) == 0) 
           { 
           <td style="width:50px;height:40px;border:solid;border-width:1px;text-align:center;font-size:small"> @Html.DisplayFormattedData(ClockingDisplay.DailyClockings[StaffClockingCount].MinInEarly)</td> 
           } 
           else 
           { 
           <td style="width:50px;height:40px;border:solid;border-width:1px;text-align:center;background-color:red; font-size:small "> @Html.DisplayFormattedData(ClockingDisplay.DailyClockings[StaffClockingCount].MinInEarly)</td> 
           } 


           StaffClockingCount++; 
           if (StaffClockingCount > ClockingDisplay.DailyClockings.Count - 1) 
           { 
            StaffClockingCount = ClockingDisplay.DailyClockings.Count - 1; 
           } 
          } 
          else 
          { 
           <td style="width:50px;height:40px;border:solid;border-width:1px;background-color:blue;"></td> 
           <td style="width:50px;height:40px;border:solid;border-width:1px;background-color:blue;"></td> 
          } 

         } 
         else 
         { 
          <td style="width:50px;height:40px;border:solid;border-width:1px;background-color:blue;"></td> 
          <td style="width:50px;height:40px;border:solid;border-width:1px;background-color:blue;"></td> 
         } 

         ColumnCount++; 
        } while (ColumnCount < TotalColumnCount); 
       </tr> 

      } 


     </tbody> 
    </table> 
</div> 

関連する問題