Only parameterless constructors and initializers are supported in LINQ to Entities

后端 未结 14 1357
暗喜
暗喜 2020-11-27 15:27

I have this error in this linq expression :

var naleznosci = (from nalTmp in db.Naleznosci
                              where nalTmp.idDziecko == idDziec
           


        
14条回答
  •  [愿得一人]
    2020-11-27 16:11

    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.

提交回复
热议问题