2016-11-27 8 views
0

別のリスト内のリストプロパティをフィルタリングする際に問題があります。 私はLinqを使用しようとしています。他のリスト内のリストをフィルタリングする方法MVC LINQ

私は文書のリストを持っていますが、それぞれには著者のリストがあります。 私は、私が筆者の姓を等しくしたいというビューから取得するsearchStringパラメータを持っています。私はクラスのドキュメントの他のプロパティでそれを行うことができましたが、私はリストでそれを行うことはできません。

これは私のコードです:

public ActionResult Index(string searchBy,string searchString) 
{ 
    db.Documentos.Include(l => l.Pais); 
    db.Documentos.Include(l => l.Etiquetas); 
    db.Documentos.Include(l => l.Autores); 
    var documentos = from m in db.Documentos select m; 
    if (searchBy=="Titulo"&& !string.IsNullOrEmpty(searchString)) 
    { 
     documentos = documentos.Where(s => s.Titulo.Contains(searchString)); 
    } 
    if(searchBy =="Fecha" && !string.IsNullOrEmpty(searchString)) 
    {     
     documentos = documentos.Where(s => s.Fecha.ToString().Contains(searchString)); 
    } 
    if(searchBy == "Autor" && !string.IsNullOrEmpty(searchString)) 
    { 
     documentos = documentos.Where(x => documentos.All(y => y.Autores.All(a => a.Apellido.Contains(searchString)))); 

    // this doesn't return anything, I cant filter with this. 
    } 
    return View(documentos.ToList()); 
} 

答えて

0

あなたはAnyメソッドを使用する必要があります。

documentos = documentos.Where(d => d.Authors.Any(f => f.LastName == searchString)); 

これは、著者の姓が検索文字列と等しい文書のリストを提供します。姓、部分文字列の正確な値を探していない場合は、Containsメソッドを使用できます。

documentos = documentos.Where(d => d.Authors 
            .Any(f => f.LastName.Contains(searchString))); 
+1

ありがとうございます!出来た!! – EmiL

+1

@ EmiLもしうまくいけば、あなたはおそらく受け入れられた答えをマークして他の人に役立つようにしたいでしょう。ここに答えを受け入れることが良い理由については、より多くの情報があります:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – CodingYoshi

+0

私は知らなかった、情報ありがとう:) – EmiL

関連する問題