获取错误:无法在LINQ to Entities查询中构造实体或复杂类型

时间:2013-05-09 10:13:09

标签: c# asp.net-mvc-3 linq

我创建了一个viewmodel: -

public class viewdata : Tool.Models.Administration.User
    {
        public string Categories { get; set; }
        public int RegionID { get; set; }
        public string RegionName { get; set; }
    }

它首先在Controller中创建。然后我将其位置更改为Tool.Models.Derived.viewdata ..然后在控制器中的Lists Actionresult内部出现错误

public ActionResult Lists(int? page)
            {
                var ListIFC = (from f in _db.Users                          
                               where f.IsActive == 1 && f.UserType == 2
                               select new viewdata
                               {
                                   UserName=f.UserName,
                                   FirstName = f.FirstName,
                                   UserID=f.UserID
                               });
                List<Tool.Models.Derived.viewdata> obj = (List<Tool.Models.Derived.viewdata>)ListIFC.ToList();
                int pageSize = 3;
                int pageNumber = (page ?? 1);

               return View(obj.ToPagedList(pageNumber, pageSize));     
            }   

我得到这样的错误......

The entity or complex type 'Tool.Models.viewdata' cannot be constructed in a LINQ to Entities query.

1 个答案:

答案 0 :(得分:0)

发表声明:

select new viewdata

您正在尝试将查询结果投影到源自实体框架的类viewdata。您在LINQ to entity投影无法做到。相反,您可以创建一个临时类并将项目投射到anonymous type或项目。