2017-07-06 92 views
0

我使用下面的代碼,以避免重複:Lambda表達式:如何映射到List而不是IEnumerable?

private static Expression<Func<Incident_Log, IncidentVM>> mappingIncidentVM() 
     { 
      return x => new IncidentVM 
      { 
       incident_ID = x.incident_id,      
       incident_description = x.incident_description, 
       file_names = x.file_names, 
       location = x.location, 

       Actions = 
        x.Action_Log.Where(y => y.assigned_to == y.assigned_to).Select(z => new ActionVM 
       { 
        incident_ID = z.incident_id, 
        action_ID = z.action_id, 
        action_description = z.action_description 

       })     
       //actionList = new List<ActionVM>(d => mappingActionVM) 

      }; 
     } 

然後,我可以做到以下幾點:

List<IncidentVM> incidents = db.Incident_Log.Select(mappingIncidentVM).ToList();

,並將其映射的Incident_Log實體到我的視圖模型IncidentVM。 我的問題是,我希望我能直接映射到一個列表,而不是一個IEnumerable.Something這樣的:

private static Expression<Func<Incident_Log, IncidentVM>> mappingIncidentVM() 
     { 
      return x => new IncidentVM 
      { 
       incident_ID = x.incident_id,     
       incident_description = x.incident_description, 
       file_names = x.file_names, 
       location = x.location, 

       actionList = 
        x.Action_Log.Where(y => y.assigned_to == y.assigned_to).Select(z => new ActionVM 
        { 
         incident_ID = z.incident_id, 
         action_ID = z.action_id, 
         action_description = z.action_description 

        }).ToList() 


      }; 
     } 

但是,這是拋出一個錯誤:

LINQ to Entities does not recognize the method System.Collections.Generic.List[ViewModel.ActionVM] ToList[ActionVM](System.Collections.Generic.IEnumerable([ViewModel.ActionVM]) method, and this method cannot be translated into a store expression.

的ToList()不被認可。

這裏是我的ViewModel:

public class IncidentVM 
    { 

     public int incident_ID { set; get; }    

     [Display(Name = "Description*")] 
     [Required] 
     public string incident_description { get; set; } 

     [Display(Name = "Specific Location*")] 
     [Required] 
     public string location { set; get; } 

     public string file_names { set; get; } 

//Here is the problem, I 'd like to have only actionList 
     public IEnumerable<ActionVM> Actions { get; set; }   
     public List<ActionVM> actionList { get; set; } 

這裏是爲Action_Log對象模型:

public partial class Action_Log 
{ 
    public int action_id { get; set; } 
    public int incident_id { get; set; } 
    public string action_description { get; set; } 

    public virtual Incident_Log Incident_Log { get; set; }  
} 

是否有可能直接映射到在這種情況下,一個列表?

+0

'y.assigned_to == y.assigned_to'? –

+0

Action_Log是否在部分類的其他部分中實現IQueryable?你如何在其上執行查詢?你可以編輯OP來顯示部分類的其他部分嗎? –

+0

@GertArnold我從模型中拿走了一些屬性來縮短代碼... assigned_to是其中的一個(只是一個字符串) – Sychal

回答

2

的問題是,LINQ無法翻譯ToList所以只是去爲IEnumerable,然後在本地處理數據時(已執行查詢後)使用ToList,你應該罰款。

+0

問題是*特別*詢問如何*避免*需要在執行外部查詢後將內部查詢實現到列表,並且特別提到它是當前的內容這樣做。 – Servy

+0

答案的第二部分指出,LINQ不能做到這一點。或者我錯了? @Servy – Sid

+0

這個問題表明,他們在嘗試做這件事時遇到了錯誤,他們想知道如何糾正這個問題並讓它真正起作用。 – Servy

-1

嘗試

actionList = 
     x.Action_Log.Where(y => y.assigned_to == y.assigned_to).Select(z => new ActionVM 
     { 
      incident_ID = z.incident_id, 
      action_ID = z.action_id, 
      action_description = z.action_description 

     }).ToList<ActionVM>() 
+1

這不會改變任何東西。問題不在於無法推斷泛型類型參數,而是由於查詢提供者無法翻譯它。 – Servy

+0

我只是試過這個,但我得到了同樣的錯誤。 – Sychal

+0

那麼我們不需要知道'Action_Log'的類型嗎? –

相關問題