创建匿名类型列表并传递给视图

时间:2019-01-26 18:40:02

标签: c# asp.net linq-to-entities

我想显示父表中的所有项以及子表中的选定项,并创建它们的列表以传递到我的视图。

这是我的动作方法

public ActionResult Index()
{
    int Month = DateTime.Now.Month;
    List<EmployeeAtt> empWithDate = new List<EmployeeAtt>();
    var employeelist = _context.TblEmployee.ToList();

    foreach (var employee in employeelist)
    {
        // var employeeAtt = new EmployeeAtt();
        var employeeAtt = _context.AttendanceTable
            .GroupBy(a => a.DateAndTime.Date)
            .Select(g => new
            {
                Date = g.Key,
                Emp_name = employee.EmployeeName,
                InTime = g
                    .Where(e => e.ScanType == "I")
                    .Min(e => e.DateAndTime.TimeOfDay),
                OutTime = g
                    .Where(e => e.ScanType == "O")
                    .Max(e => e.DateAndTime.TimeOfDay),
            });
    }
    return View();
} 

这是我的观点

 @model IEnumerable<Attendance.Models.EmployeeAtt>
    @{`enter code here`
        ViewBag.Title = "AttendanceTable";
        <!--Get number of days of current month-->
        var DaysInmonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
        <!--Create a CurrentName field-->
        var CurrentName = "";
    }

    <table class="table table-bordered">
        <thead>
            <tr>
                <th>EmpName</th>
                <!--Loop all the days of month and print it-->
                @for (var numb = 1; numb <= DaysInmonth; numb++)
                {
                    <th>@numb</th>
                }

            </tr>
        </thead>
        <tbody>
            <!--Loop model-->
            @foreach (var emp in Model)
            {
                //if Name is repeated, skip
                if (CurrentName != emp.Emp_name)
                {
                    // Set Name
                    CurrentName = emp.Emp_name;
            <tr>
                <!--print employee name one time only at the start of row-->
                <td>@emp.Emp_name</td>

                <!--loop all days of month-->
                @for (var numb = 1; numb <= DaysInmonth; numb++)
                {
                    <td>
                        @{
                            <!--print only that date time value which is equal to current date(as it will match column header) and current employee name, else print empty-->
                            var GetThatDayValue = Model.Where(a => a.Date.Value.Day == numb && a.Emp_name == emp.Emp_name).FirstOrDefault();
                            var DD = GetThatDayValue != null ? GetThatDayValue.InTime + " " + GetThatDayValue.OutTime : "A";
                            <text> @DD </text>
                        }
                    </td>    
            }
            </tr>
                }
            }
        </tbody>
    </table>

如何从匿名类型转换为具体类型,以便我可以列出视图模型对象(EmployeeAtt)并在我的视图中访问它

1 个答案:

答案 0 :(得分:1)

如果仅在编译时选择具体类型,为什么需要创建匿名对象?

因此基本上,您可以在“选择”中执行此操作:

  0.00  0.010000
  0.02  0.030000
  0.04  0.050000
  0.06  0.070000
  0.08  0.090000
  0.10  0.110000
  0.12  0.130000
  0.14  0.150000
  0.16  0.170000
  0.18  0.190000
  0.20  0.210000
  0.22  0.230000
  0.24  0.250000
  0.26  0.270000
  0.28  0.290000
  0.30  0.310000
  0.32  0.330000
  0.34  0.350000
  0.36  0.370000
  0.38  0.390000
  0.40  0.410000
  0.42  0.430000
  0.44  0.450000
  0.46  0.470000
  0.48  0.490000
  0.50  0.510000
  0.52  0.530000
  0.54  0.550000
  0.56  0.570000
  0.58  0.590000
  0.60  0.610000
  0.62  0.630000
  0.64  0.650000
  0.66  0.670000
  0.68  0.690000
  0.70  0.710000
  0.72  0.730000
  0.74  0.750000
  0.76  0.770000
  0.78  0.790000
  0.80  0.810000
  0.82  0.830000
  0.84  0.850000
  0.86  0.870000
  0.88  0.890000
  0.90  0.910000
  0.92  0.930000
  0.94  0.950000
  0.96  0.970000
  0.98  0.990000
  1.00  0.000000

如果您不知道确切的类型并且想在运行时进行转换,则可以尝试将.Select(g => new EmployeeAtt { /* fill in properties */ }); Convert.ChangeType一起使用

TypeDescriptor.GetConverter