I have this error in this linq expression :
var naleznosci = (from nalTmp in db.Naleznosci
where nalTmp.idDziecko == idDziec
yeh, try it like this....
var naleznosci = (from nalTmp in db.Naleznosci
where nalTmp.idDziecko == idDziec
select new Payments()
{
Dziecko.Imie,
Dziecko.Nazwisko,
Miesiace.Nazwa,
Kwota,
RodzajeOplat.NazwaRodzajuOplaty,
RodzajeOplat.TypyOplat.NazwaTypuOplaty,
DataRozliczenia,
TerminPlatnosci
}).ToList();
this will new up your Payment object using a parameterless constructor, and then initialize the properties that are listed inside the curly braces { }
Just ToList()
the DbSet
before the Select
statement..
the actual DbSet
is saved as a query, it's not fulfilled yet.
After calling ToList()
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.
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:
var result = myContext.SomeModelClass.AsEnumerable().Select(m => m.ToString());
It will work fine. However, it will, of course, loose the benefits of native queries.
Also, if you want to use a constructor with multiple objects to initialize, you might get error if no values are returned by Linq.
So you might want to do something like this:
(from x in table_1
join y in table_2
on x.id equals y.id
select new {
val1 = x,
val2 = y
})
.DefaultIfEmpty()
.ToList()
.Select(a => new Val_Constructor(a.val1 != null ? a.val1 : new Val_1_Constructor(),
a.val2 != null ? a.val2 : new Val_2_Constructor()))
.ToList();
If you're like me and don't want to have to populate your properties for each query you're building, there is another way to solve this issue.
var query = from orderDetail in context.OrderDetails
join order in context.Orders on order.OrderId equals orderDetail.orderId
select new { order, orderDetail };
At this point you have an IQueryable containing an anonymous object. If you want to populate your custom object with a constructor you can simply do something like this:
return query.ToList().Select(r => new OrderDetails(r.order, r.orderDetail));
Now your custom object (which takes two objects as a parameter) can populate your properties as needed.
First I would avoid the solution with
from ....
select new Payments
{
Imie = nalTmp.Dziecko.Imie,
....
}
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:
from ....
select new
{
Imie = nalTmp.Dziecko.Imie,
Nazwisko = nalTmp.Dziecko.Nazwisko
....
}
.ToList() // Here comes transfer to LINQ to Collections.
.Select(nalImp => new Payments
(
nalTmp.Imie,//assume this is a required field
...........
)
{
Nazwisko = nalTmp.Nazwisko //optional field
})
.ToList();