Only parameterless constructors and initializers are supported in LINQ to Entities message

前端 未结 4 1032
失恋的感觉
失恋的感觉 2020-12-03 00:55

I have a method that returns data from an EF model.

I\'m getting the above message, but I can\'t wotk our how to circumvent the problem.

    public s         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 01:40

    Message is clear : linq to entities doesn't support objects without a parameterless ctor.

    So

    Solution1

    enumerate before (or use an intermediate anonymous type and enumerate on that one)

    .ToList()
    .Select(x => new FundedCount(
                        x.Key.BuyerId,
                        x.Count() / 30 * daysInMonth))
                    .ToList();
    

    Solution2

    add a parameterless ctor to your FundedCount class (if it's possible)

    public FundedCount() {}
    

    and use

    .Select(x => new FundedCount{
                             = x.Key.BuyerId,
                             = x.Count() / 30 * daysInMonth
                             })
                        .ToList();
    

提交回复
热议问题