2017-07-06 2 views
0

私は重複を避けるために、次のコードを使用しています:ラムダ式:IEnumerableではなくListにどのようにマップするのですか?

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

そして、それは私のViewModel IncidentVMにIncident_Logエンティティをマッピングします。その後、私は次の操作を行うことができます

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) 

      }; 
     } 

。 )

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メソッド(:

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


      }; 
     } 

しかし、それはエラーを投げている: 私の問題は、私は私はこのようなリストではなくIEnumerable.Somethingに直接マッピングことがしたいということです認識されません。ここで

は私の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はその1つ(文字列) – Sychal

答えて

2

を試してみてください。

+0

質問は外部クエリが実行された後に内部クエリをリストにマテリアライズする必要性を避ける*具体的に*質問しています。やって – Servy

+0

答えの2番目の部分は、LINQがそれを行うことができないと述べています。または私は間違っていますか? @Servy – Sid

+0

質問には、その試みに間違いがあり、問題を修正して実際に動作させる方法を知りたいという質問があります。 – Servy

-1

は、問題がLINQはToListがこれだけIEnumerableのために行くと、ローカルのデータで作業する際ToListを使用(クエリを実行した後に)、あなたは問題ないはず変換できないということです

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'の型を知る必要はありませんか? –

関連する問題