简体   繁体   中英

Getting error:The entity or complex type cannot be constructed in a LINQ to Entities query

I have created a viewmodel:-

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

It was first created inside Controller..Then i changed its location to Tool.Models.Derived.viewdata..Then am getting error inside Lists Actionresult in controller

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

Am getting an error like this...

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

With your statement:

select new viewdata

You are trying to project your query result to your class viewdata which is deriving from Entity framework. You can't do that in LINQ to entities projection. Instead you may create a temporary class and project to that or project to anonymous type .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM