2013-05-21 82 views
0

目前固定我的代碼直到出現異常System.NotSupportedException:實體或複雜類型Model.APPLICANT不能在LINQ構建以查詢實體

System.NotSupportedException: The entity or complex type Model.APPLICANT' cannot be constructed in a LINQ to Entities query

這是我的控制器:

public IEnumerable<APPLICANT> GetApplicant() 
{ 
    IEnumerable<APPLICANT> applicantdata = Cache.Get("applicants") as IEnumerable<APPLICANT>; 


    if (applicantdata == null) 
    { 

     var applicantList = (from app in context.APPLICANTs 
          join a in context.Profiles 
          on app.Profile_id equals a.PROFILE_ID into output 
          from j in output.DefaultIfEmpty() 
          select new APPLICANT() { APPLICANT_ID = app.APPLICANT_ID, APPLICANT_LastName = (j == null ? app.APPLICANT_LastName : j.Applicant_LASTNAME) }).Take(1000).AsEnumerable().AsQueryable(); 

     applicantdata = applicantList.Where(v => !String.IsNullOrEmpty(v.APPLICANT_LastName)).AsEnumerable(); 



     if (applicantdata.Any()) 
     { 
      Cache.Set("applicants", applicantdata, 30); 
     } 
    } 
    return applicantdata; 

} 

唯一的例外出現在該行

if (applicantdata.Any())

我希望有人可以建議或可以找到一種方法來解決這個問題。 。謝謝

+0

的可能的複製[實體或複雜類型...不能在LINQ被構造爲實體查詢]( http://stackoverflow.com/questions/28985083/the-entity-or-complex-type-cannot-be-constructed-in-a-linq-to-entities-query) – Sparrow

回答

1

由於無法在查詢中創建非EF類型的新實例,因此可以將查詢分爲兩部分。

首先你得到的數據

var data = from app in context.APPLICANTs 
      join a in context.Profiles 
      on app.Profile_id equals a.PROFILE_ID into output 
      from j in output.DefaultIfEmpty() 
      select new { 
      Id = app.APPLICANT_ID, 
      LastName = 
       (j == null ? app.APPLICANT_LastName : j.Applicant_LASTNAME) 
      }; 

var applicantData = data.Take(1000) 
    .Where(v => !String.IsNullOrEmpty(v.APPLICANT_LastName)); 

然後你初始化實例

var applicants = (from a in applicantData 
        select new APPLICANT() { 
        APPLICANT_ID = a.Id, 
        APPLICANT_LastName = a.LastName 
        } 
       ).AsEnumerable(); 
+0

''AnonymousType#1'不包含'Applicant_LastName'的定義和沒有接受第一個參數o的擴展方法'Applicant_LastName'可以找到f類型'AnonymousType#1'(你是否遺漏了使用指令或程序集引用?)'在IsNullOrEmpty(v.Applicant_Lastname)上發生錯誤 –

+0

更改'.Where(v =>!String.IsNullOrEmpty(v。 (v =>!String.IsNullOrEmpty(v.LastName))' –

+0

感謝您的修正! –

相關問題