指定的类型成员' FullName' LINQ to Entities不支持

时间:2017-03-15 14:02:33

标签: c# asp.net-mvc entity-framework linq

我有一个带有服务器端处理的jQuery DataTable,当我Administrator = a.Administrator.FullName使用data时,这会给我一个异常错误。

  

指定的类型成员' FullName' LINQ to Entities

不支持

我的Administrator课程中有Department字段,但仍然收到此错误消息。有没有办法解决这个错误?

部门模型

public class Department
    {
        public int DepartmentID { get; set; }

        public string Name { get; set; }

        public decimal Budget { get; set; }

        public DateTime StartDate { get; set; }

        [Display(Name = "Administrator")]
        public int? InstructorID { get; set; }

        public virtual Instructor Administrator { get; set; }
        public virtual ICollection<Course> Courses { get; set; }

讲师班

public class Instructor : Person
    {
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Hire Date")]
        public DateTime HireDate { get; set; }

        public virtual ICollection<Course> Courses { get; set; }
        public virtual OfficeAssignment OfficeAssignment { get; set; }
    }

Person Class具有FullName字段

[Display(Name = "Full Name")]
        public string FullName
        {
            get
            {
                return LastName + ", " + FirstMidName;
            }
        }

部门总监

public ActionResult DataHandler([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
        {
            //IQueryable<Department> query = departmentService.GetDepartments();

            IQueryable<Department> query;

            using (SchoolContext db = new SchoolContext())
            {
                query = db.Departments;

                var totalCount = query.Count();

                // Apply filters for searching
                if (requestModel.Search.Value != string.Empty)
                {
                    var value = requestModel.Search.Value.Trim();
                    query = query.Where(p => p.Name.Contains(value)); //||
                    //p.Administrator.FullName.Contains(value));
                }

                var filteredCount = query.Count();

                // Sorting
                var sortedColumns = requestModel.Columns.GetSortedColumns();
                var orderByString = String.Empty;

                foreach (var column in sortedColumns)
                {
                    orderByString += orderByString != String.Empty ? "," : "";
                    orderByString += (column.Data) +
                      (column.SortDirection ==
                      Column.OrderDirection.Ascendant ? " asc" : " desc");
                }

                query = query.OrderBy(orderByString ==
                string.Empty ? "BarCode asc" : orderByString);

                // Paging
                query = query.Skip(requestModel.Start).Take(requestModel.Length);

                var data = query.Select(a => new 
                {
                    Name = a.Name,
                    Budget = a.Budget.ToString(),
                    StartDate = a.StartDate.ToString(),
                    Administrator = a.Administrator.FullName
                }).ToList();

                return Json(new DataTablesResponse
                (requestModel.Draw, data, filteredCount, totalCount),
                            JsonRequestBehavior.AllowGet);
            }
        }

1 个答案:

答案 0 :(得分:0)

正如@IvanStoev所提到的,FullName未被映射,因此对于我的data,我必须使用FirstNameLastName字段。

Administrator = a.Administrator.FirstMidName + " " + a.Administrator.LastName