2016-11-27 2 views
0

다른 목록의 목록 속성을 필터링하는 데 문제가 있습니다. Linq를 사용하려고합니다.다른 목록의 목록을 필터링하는 방법 MVC LINQ

문서 목록이 있습니다. 각 목록에는 작성자 목록이 있습니다. 작성자의 성을 같게하는 view에서 얻는 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

관련 문제