2017-07-06 56 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'的类型吗? –