我有一个如下所示的模型:
public class MyType{
public string Id {get;set;}
public string Name{get;set;}
public List<MyType> Children{get;set;}
}
在我的数据中,我只有两个层级的数据,这意味着我的对象看起来像:
{
MyType{"1","firstParent",
{
MyType{"2","firstChild",null},
MyType{"3","secondChild",null}}
},
MyType{"4","secondParent",
{
MyType{"5","firstChild",null},
MyType{"6","secondChild",null}}
}
}
我该如何查询具有特定ID的MyType对象,其中它可能是父对象或子对象?
以下代码仅返回父对象。
collection.FirstOrDefault(c => c.id==id)