带有列表和非列表项的C#类

时间:2017-11-16 10:49:48

标签: c#

如何将数据添加到以下类列表中。两个列表都是动态的,并且每个时钟列表项的每日时钟列表可以是不同的。 我现在能够为每个列表,时钟和日常时钟添加数据,但是每日时钟中的数据并没有通过时钟[1] .dailyclockings [1]。我只能在时钟[1] .name和dailyclockings [1] .clockingdate中看到数据,但如果我显示时钟[1] .dailyclocking [1] .clockingdate,则该值为null。

我试图实现的目标如下

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

3 个答案:

答案 0 :(得分: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))
            {}

可能存在错误或db Null,因此您的某个列表不会被填充,您甚至不会知道它,这将导致空值

2)不要初始化列表中的对象:

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

在实例化新对象的过程中,您可能会丢失数据。

像这样使用:

f.DailyClockings dailyClockingsList = new List<DailyClockings> 

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

答案 1 :(得分:0)

对不起,如果我错了,我会发表评论,但我的声誉限制了我。

无论如何,在这部分:

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

看起来好像你拿Clockings list,将DailyClocking list作为对象添加到列表中,然后将Clockings对象添加到列表中。

它应该是这样的吗? :

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

答案 2 :(得分: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>