"Every zoo"
click
Suppose you have a list of zoos:
List<Zoo> zooList = GetZooList();
Then, if you want some great animals from all the zoos, you would apply SelectMany this way:
List<string> animalList = zooList .SelectMany(zoo => zoo.animals) .Distinct() .ToList();
And if you usually did this task and wanted one function to wrap these three calls, you can write such a function as follows:
public static List<string> GetDistinctStringList<T>( this IEnumerable<T> source, Func<T, IEnumerable<string>> childCollectionFunc ) { return source.SelectMany(childCollectionFunc).Distinct().ToList(); }
which will then be called:
List<string> animals = ZooList.GetDistinctStringList(zoo => zoo.animals);
And for sample code that does not compile (for which you did not specify an error message), I output to you that you need to add ToList ():
.OrderBy(s => s).ToList();
Another problem (why the type argument cannot be inferred) is that string[] does not implement IEnumerable<string> . Change this parameter to IEnumerable<string> instead of string[]
Amy b source share