I have this error in this linq expression :
var naleznosci = (from nalTmp in db.Naleznosci
where nalTmp.idDziecko == idDziec
select new Payments
(
nalTmp.Dziecko.Imie,
nalTmp.Dziecko.Nazwisko,
nalTmp.Miesiace.Nazwa,
nalTmp.Kwota,
nalTmp.RodzajeOplat.NazwaRodzajuOplaty,
nalTmp.RodzajeOplat.TypyOplat.NazwaTypuOplaty,
nalTmp.DataRozliczenia,
nalTmp.TerminPlatnosci
)).ToList();
Any idea how solve this problem? I try with any combination of expression... :/
First I would avoid the solution with
This requires an empty constructor and ignores encapsulation so you are saying new Payments() is a valid payment without any data, but instead the object must have at least a value and probably other required fields depending on your domain.
It's better to have a constructor for required fields but only bring needed data:
Although it is late to answer, it could still help someone in distress. Since LINQ to entities do not support the parameter-less object constructions. However, the projection methods for IEnumerable.
So prior to selection, just convert your IQueryable to IEnumerable by using this code:
It will work fine. However, it will, of course, loose the benefits of native queries.
If you still want to use your constructor for initialization and not properties (sometimes this behaviour is desired for initialization purposes), enumerate the query by calling
ToList()
orToArray()
, and then useSelect(…)
. Thus it will use LINQ to Collections and that limitation of not being able to call constructor with parameters inSelect(…)
will vanish.So your code should look something like this:
yeh, try it like this....
this will new up your Payment object using a parameterless constructor, and then initialize the properties that are listed inside the curly braces
{ }
Having just encountered this error myself, I thought I would add that if the
Payment
type is astruct
, you would also encounter the same error becausestruct
types do not support parameterless constructors.In that event, converting
Payment
to a class and using the object initializer syntax will resolve the issue.Just
ToList()
theDbSet
before theSelect
statement.. the actualDbSet
is saved as a query, it's not fulfilled yet. After callingToList()
you're playing with objects, and then you can use a non-default constructor in the query.Not the most efficient way usage-time wise, but it's an option on small sets.