geoff swartz
geoff swartz

Reputation: 5965

parameterless constructor issue with entity framework

I have the following code that is causing the parameterless constructor error with entity framework. I've figured out that it's being caused by the if (search.Active) block and the dates inside there... but I'm not sure how to get around it. How can I construct my dates so that EF will work with them? Thanks.

var members = from m in Members select m;

if (!string.IsNullOrEmpty(search.Letter))
            members = members.Where(x => x.LastName.Substring(0, 1) == search.Letter.Substring(0, 1));

        if (search.Active)
        {
            if (DateTime.Now < new DateTime(DateTime.Now.Year, 10, 15))
            {
                members = members.Where(x => x.ExpireDate >= new DateTime(DateTime.Now.Year, 5, 31));
            }
            else
            {
                members = members.Where(x => x.ExpireDate >= new DateTime(DateTime.Now.Year + 1, 5, 31));
            }
        }

        return members.Select(x => new MemberListItem
            {
                FirstName = x.FirstName, 
                LastName = x.LastName, 
                MemberId = x.MemberId, 
                ExpirationDate = x.ExpireDate
            }).ToList();

Upvotes: 1

Views: 283

Answers (1)

Slauma
Slauma

Reputation: 177153

It's possible that this will solve the problem because EF might have problems to construct the DateTime inside of a LINQ-to-Entities query:

if (search.Active)
{
    if (DateTime.Now < new DateTime(DateTime.Now.Year, 10, 15))
    {
        DateTime date = new DateTime(DateTime.Now.Year, 5, 31);
        members = members.Where(x => x.ExpireDate >= date);
    }
    else
    {
        DateTime date = new DateTime(DateTime.Now.Year + 1, 5, 31);
        members = members.Where(x => x.ExpireDate >= date);
    }
}

Upvotes: 1

Related Questions