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

creating a list of anonymous type and passing to view

我想显示我的 parent table 中的所有项目以及 child table 中的选定项目,并创建一个列表以传递到我的视图。

这是我的操作方法

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>

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

如果在编译时可以 select 具体类型,为什么还需要创建匿名对象?

所以基本上你可以在 Select:

.Select(g => new EmployeeAtt { /* fill in properties */ });

如果您不知道确切的类型并想在运行时进行转换,您可以尝试使用 Convert.ChangeTypeTypeDescriptor.GetConverter

Convert.ChangeType(TypeDescriptor.GetConverter(objectType).ConvertFrom(anonymousObject), objectType));