Tuesday, March 5, 2013

Only parameterless constructors and initializers are supported in LINQ to Entities

This is the error that usually come up when you try to run a LINQ Query and return an object out of this QUERY.

Something like the below example:
var query = (from c in ctx.Clients
                         where l.UserID == userid
                         select new ClientList(c.ClientFirstName, c.ClientLastName,  c.ClientEmail)
                          ).ToList();
Well the Error is Pretty much self explanatory. No Parameterized constructors are allowed for LINQ to Entities...

Workaround ?
Change your Query to something like this..
var query = (from c in ctx.Clients            
                         where l.UserID == userid
                         select new ClientList{ClientFirstName= c.ClientFirstName, ClientLastName= c.ClientLastName, ClientEmail= c.ClientEmail}
                          ).ToList();