2013-05-09 9 views
5

ho il prossimo arhitecture:Selezionare proprietà di un oggetto che si trova in un elenco di oggetti che è anche in un altro elenco di oggetti

public class Element 
{ 
    public uint Id { get; set; } 
    public ICollection<ElementDetails> elementDetails { get; set; } 
} 
public class ElementDetails 
{ 
    public string ElementTitle { get; set; } 
    public string Content { get; set; } 
} 

E c'è List<Element> someList che contiene centinaia di elementi. Sto cercando di ottenere un elenco di ElementTitle (stringhe) che contiene un determinato testo (l'ho chiamato "seed"). Quello che voglio realizzare è un typeahead. Ecco il mio tentativo:

List<Element> suggestedElements = someList.Where(s => s.elementDetails.Any(ss => ss.ElementTitle.Contains(seed))).ToList(); 
List<string> suggestions = suggestedElements .SelectMany(t => t.elementDetails.Select(x => x.ElementTitle)).ToList() }); // contains all ElementTitle, including those ElementTitle that don't contain the "seed"... 

Come posso eliminare quegli elementi che non contengono il seme?

risposta

12
List<string> suggestions = someList.SelectMany(x => x.elementDetails) 
            .Where(y => y.ElementTitle.Contains(seed)) 
            .Select(z => z.ElementTitle) 
            .ToList(); 

Ancora più semplice:

List<string> suggestions = someList.SelectMany(x => x.elementDetails) 
            .Select(y => y.ElementTitle); 
            .Where(z => z.Contains(seed)) 
            .ToList(); 
+1

Grazie uomo! Funziona come un fascino! – VladN

Problemi correlati