Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从linq查询中展平结果_C#_Linq_C# 4.0 - Fatal编程技术网

C# 从linq查询中展平结果

C# 从linq查询中展平结果,c#,linq,c#-4.0,C#,Linq,C# 4.0,我使用linq查询来获取从某个指定类派生的所有类的所有属性的名称,如下所示: /// <summary> /// Gets all visible properties of classes deriving from the specified base type /// </summary> private HashSet<string> GetDerivedPropertyNames(Type baseType)

我使用linq查询来获取从某个指定类派生的所有类的所有属性的名称,如下所示:

    /// <summary>
    /// Gets all visible properties of classes deriving from the specified base type
    /// </summary>
    private HashSet<string> GetDerivedPropertyNames(Type baseType)
    {
        // get all property names of the properties of types that derive from the base type
        var propertyNames =
            from type in baseType.Assembly.GetTypes()
            where type.IsSubclassOf(baseType)
            select Array.ConvertAll(type.GetProperties(), property => property.Name);
        // the hashset constructor will filter out all duplicate property names
        return new HashSet<string>(propertyNames);
    }
//
///获取从指定基类型派生的类的所有可见属性
/// 
私有哈希集GetDerivedPropertyNames(类型baseType)
{
//获取从基类型派生的类型的属性的所有属性名
var propertyNames=
来自baseType.Assembly.GetTypes()中的类型
其中type.IsSubclassOf(baseType)
选择Array.ConvertAll(type.GetProperties(),property=>property.Name);
//hashset构造函数将过滤掉所有重复的属性名
返回新的哈希集(propertyNames);
}

但是,这不会编译,因为linq查询的结果是一个
IEnumerable
,而我想要一个
IEnumerable
。如何将结果展平为单个
IEnumerable

我想您可能希望
选择多个

var propertyNames = baseType.Assembly.GetTypes()
 .Where(type => type.IsSubclassOf(baseType))
 .SelectMany(type => type.GetProperties().Select(property => property.Name));
可能的话,立即包括不同的步骤:

var propertyNames = baseType.Assembly.GetTypes()
 .Where(type => type.IsSubclassOf(baseType))
 .SelectMany(type => type.GetProperties().Select(property => property.Name))
 .Distinct();

那么,不同的O(n)与HashSet构造函数类似吗?@mtijn你可以打赌。或者你可以对它进行分析。请注意,严格来说,无论集合类型如何,从IEnumerable插入集合都将是O(n)。你的意思可能是:你确定“在最坏的情况下,Distinct不会去O(n*n)”。你可以打赌。为什么微软(或Mono)在实现这一点时会犯如此严重的错误?