如何使用LINQ将Type的嵌套数据结构转换为Type

时间:2012-05-30 10:49:06

标签: c# linq algorithm

我有Folowing Types:

class TypeA
{
    public string Name { get; set; }
    public object Value { get; set; } // Holds either a primitive DataType or a List<TypeA>
    public string IrrelevantInformation { get; set; }
}

class TypeB
{
    public string Name { get; set; }
    public object Value { get; set; } //Holds either a primitive DataType or a List<TypeB>
}

我想要的是将TypeA的Hirarchical结构转换为TypeB。

我使用传统的递归方法做到了:

private TypeB ConvertToTypeB(TypeA Input)
{
    return new TypeB() { Name = Input.Name, Value = ((Input.Value is List<TypeA>) ? ((List<TypeA>)Input.Value).Select(v=>ConvertToTypeB(v)).ToList() : Input.Value) };
}

我的问题是:只使用一个Linq查询,可以在没有ConvertToTypeB函数的情况下完成吗?

1 个答案:

答案 0 :(得分:1)

要转换hieractical结构,需要递归调用。 没有办法排除AtoB方法。

相关问题