1

我有以下代码导致实体框架出现无参数构造函数错误。我发现它是由 if (search.Active) 块和里面的日期引起的......但我不知道如何解决它。如何构建我的日期以便 EF 与他们合作?谢谢。

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();
4

1 回答 1

1

这可能会解决问题,因为 EF 在构建DateTimeLINQ-to-Entities 查询的内部时可能会遇到问题:

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);
    }
}
于 2013-04-07T13:43:30.500 回答