通用LINQ函数 - 选择Func作为参数的SelectMany

Jon*_*way 4 c# linq-to-objects

我有一个包含许多字符串数组的类.我想要一个通用函数,它可以让我List<string>对给定的属性有一个独特的功能.例:

public class Zoo 
{    
  string Name { get; set;}
  string[] Animals { get; set;}
  string[] Zookeepers { get; set;}
  string[] Vendors { get; set;}
}
Run Code Online (Sandbox Code Playgroud)

我想要一个通用的功能,让我有一个独特List<string>的列表中的动物?我希望这是通用的,所以我也可以得到一个明确的Zookeepers和供应商列表.

我一直在尝试这个,但它没有编译:

public static List<string> GetExtendedList(Func<Zoo, string[]> filter)
{
        var Zoos = QueryZoos(HttpContext.Current);
        return Zoos.Where(z => z.Type == "Active")
            .SelectMany(filter)
            .Distinct()
            .OrderBy(s => s);
    }
Run Code Online (Sandbox Code Playgroud)

注意:这与我之前提出的两个问题有关,但我在合并信息时遇到了问题.我之前曾询问如何使用SelectMany进行查询(SO 1229897)并单独询问如何编写一个使用Select而不是SelectMany获取列表的泛型函数(SO 1278989).

Amy*_*y B 19

"每个动物园"

点击

假设你有一个动物园列表:

List<Zoo> zooList = GetZooList();
Run Code Online (Sandbox Code Playgroud)

然后,如果您想要来自所有动物园的不同动物,您将以这种方式应用SelectMany:

List<string> animalList = zooList
  .SelectMany(zoo => zoo.animals)
  .Distinct()
  .ToList();
Run Code Online (Sandbox Code Playgroud)

如果您经常执行此任务并希望一个函数包装这三个调用,您可以这样编写这样的函数:

public static List<string> GetDistinctStringList<T>(
  this IEnumerable<T> source,
  Func<T, IEnumerable<string>> childCollectionFunc
)
{
  return source.SelectMany(childCollectionFunc).Distinct().ToList();
}
Run Code Online (Sandbox Code Playgroud)

然后会被称为:

List<string> animals = ZooList.GetDistinctStringList(zoo => zoo.animals);
Run Code Online (Sandbox Code Playgroud)

对于不编译的代码示例(您没有给出错误消息),我推断您需要添加ToList():

.OrderBy(s => s).ToList();
Run Code Online (Sandbox Code Playgroud)

另一个问题(为什么不能推断出类型参数)是string[]没有实现的IEnumerable<string>.将该类型参数更改为IEnumerable<string>而不是string[]