This code builds without problems but fails when executed:
var query =
_db.STEWARDSHIP
.OrderByDescending(r => r.VisitDate)
.Where(r => SiteId == null || r.SiteAssoc.Id == iSiteId)
.Where(r => r.MarkedForDeletion == false)
.Select(r => new SearchResults(r.Id,
r.SiteAssoc.Name,
r.VisitDate,
r.VisitTypeValAssoc.Description));
The error message is:
Only parameterless constructors and initializers are supported in LINQ to Entities.
What am I doing wrong here? I must pass info to the constructor to build the SearchResults objects. Is there a way to do this or do I have to make this a 2-step thing where I get the results and then iterate over the results to create the new objects?
UPDATE:
Changing the select to this fixes the issue:
.Select(r => new SearchResults(){Id = r.Id,
Name = r.SiteAssoc.Name,
VisitDate = r.VisitDate,
VisitType = r.VisitTypeValAssoc.Description});