1

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();
2
  • The exact exception message would be more helpful than this mysterious "parameterless constructor error". Commented Apr 7, 2013 at 13:44
  • Sorry, when I searched on the error message there were a ton of hits here on stackoverflow and it was mentioned that it was a very common error. Anyway, here's the mysterious message - Only parameterless constructors and initializers are supported in LINQ to Entities. Commented Apr 7, 2013 at 16:58

1 Answer 1

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.