2013-04-07 53 views
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(); 
+0

的*確切*異常信息會更有益比這個神祕的*「無參數構造函數錯誤」*。 – Slauma 2013-04-07 13:44:14

+0

對不起,當我搜索錯誤消息時,在stackoverflow上有大量命中,並提到它是一個非常常見的錯誤。無論如何,這裏是神祕的信息 - LINQ to Entities只支持無參數的構造函數和初始值設定項。 – 2013-04-07 16:58:54

回答

1

這是可能的,這將解決這個問題,因爲EF可能有問題,構建一個LINQ到實體查詢裏面的DateTime

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); 
    } 
} 
+0

就是這樣,謝謝! – 2013-04-07 16:57:22