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

The reason it is complaining is because it doesn’t know how to translate your Select() into a SQL expression. If you need to do a data transformation to a POCO that is not an entity, you should first get the relevant data from EF and then transform it to the POCO.

In your case it should be as simple as calling ToList() earlier:

return r.Find()
        .Where(x => x.AppliedOn >= day1 && x.AppliedOn <= day31 &&
                    x.ResultTypeId == (int)MatchResultType.Accepted)
        .GroupBy(x => new { x.BuyerId, x.AppliedOn })
        .ToList() // this causes the query to execute
        .Select(x => new FundedCount(x.Key.BuyerId, x.Count() / 30 * daysInMonth));

Be careful with this, though, and make sure that you’re limiting the size of the data set returned by ToList() as much as possible so that you’re not trying to load an entire table into memory.

Leave a Comment