Question

I have a class like this:

public class FileCollection:ObservableCollection<IUserFile>.  

From within the class, I would like to get a subset of the collection, based on a list of names.

I imagine it would be something like this:

List<IUserFile> selectedFiles = new List<IUserFile>;
foreach(string s in names)  
{  
    var matchingFiles = this.SelectMany(userFile => userFile.Name.Equals(s));  
    foreach(IUserFile uf in matchingFiles)  
    {  
        selectedFiles.Add(uf);    
    }  
 }  

At this point, I'm having trouble with the Select or SelectMany call; the compiler error messages are not that helpful.
Any suggestions on how to extract the subset from the collection would be appreciated...

Was it helpful?

Solution

Use a Where instead - which "Filters a sequence of values based on a predicate" - such that:

var matches = this.Where(userFile => userFile.Name.Equals(s)); 

Rather than SelectMany, which "Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top