Only parameterless constructors and initializers a

2020-01-24 20:48发布

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... :/

13条回答
地球回转人心会变
2楼-- · 2020-01-24 21:22

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.

查看更多
登录 后发表回答