如果我有一个Collection<T>
,每个T
对象可以有一个孩子Collection<T>
以及一个Collection<T2>
,是如何找到的T2
目标的最佳途径,在那里我知道T2
对象的string name
,我有一个Collection<T>
?如何在集合中找到一个对象,其中集合具有相同类型的子集合?
1
A
回答
0
递归搜索实例级别T2集合和子T集合,直到找到匹配项。
public T2 Find(T primaryObject, string searchValue)
{
var secondaryObject = primaryObject.T2Collection.SingleOrDefault(x => x.Name == searchValue)
if (secondaryObject != null)
return secondaryObject;
foreach (var childObject in primaryObject.T1Collection)
{
secondaryObject = Find(childObject, searchValue);
if (secondaryObject != null)
return secondaryObject;
}
return null;
}
0
你需要一个递归的方法来搜索所有T2匹配搜索值:
public class Program
{
private static IEnumerable<Bar> Find(Foo foo, String name)
{
foreach (var x in foo.Bars.Where(x => x.Name == name))
yield return x;
var bars = foo.Foos?.SelectMany(x => Find(x, name)) ?? new Bar[0];
foreach (var y in bars)
yield return y;
}
public static void Main(String[] args)
{
var foo = new Foo
{
Bars = new List<Bar>
{
new Bar { Name = "n1" },
new Bar { Name = "n2" },
new Bar { Name = "n3" },
new Bar { Name = "n4" }
},
Foos = new List<Foo>
{
new Foo
{
Bars = new List<Bar>
{
new Bar { Name = "n1" },
new Bar { Name = "n2" },
new Bar { Name = "n3" },
new Bar { Name = "n4" }
},
Foos = new List<Foo>
{
new Foo
{
Bars = new List<Bar>
{
new Bar { Name = "n1" },
new Bar { Name = "n2" },
new Bar { Name = "n3" },
new Bar { Name = "n4" }
}
}
}
},
new Foo
{
Bars = new List<Bar>
{
new Bar { Name = "n1" },
new Bar { Name = "n2" },
new Bar { Name = "n3" },
new Bar { Name = "n4" }
}
}
}
};
foreach (var x in Find(foo, "n1"))
Console.WriteLine(x.Name);
Console.ReadLine();
}
}
public class Foo
{
#region Properties
public IEnumerable<Foo> Foos { get; set; }
public IEnumerable<Bar> Bars { get; set; }
#endregion
}
public class Bar
{
#region Properties
public String Name { get; set; }
#endregion
}
0
无扩展名通常可以用来代替递归函数:
using System.Reactive.Linq;
public class T2
{
public string Name;
}
public class T
{
public IEnumerable<T2> t2Collection;
public IEnumerable<T> tCollection;
}
public T2 Find(IEnumerable<T> primaryCollection, string searchValue)
{
T2 result = null;
Observable.Generate(
new
{
currentCollection = primaryCollection,
searchedT2 = default(T2)
},
state => state.searchedT2 == default(T2),
state => new
{
currentCollection = state.currentCollection.SelectMany(t => t.tCollection),
searchedT2 = state.currentCollection.SelectMany(t => t.t2Collection).FirstOrDefault(t2 => t2.Name == searchValue)
},
state => state.searchedT2
).Subscribe(t2=>result=t2);
return result;
}
相关问题
- 1. LINQ:在一个集合中搜索单个对象的集合
- 2. 从集合ArrayList中返回一个元素,其中每个集合具有不同的元素类型
- 3. 添加具有子集合的对象
- 4. 如何在Scala中创建一个类型对象的集合
- 5. Backbone中的集合和子集合(及其相关视图)
- 6. MongoDB集合可以在其中有另一个集合吗?
- 7. 集合中的一个集合
- 8. LINQ查找集合中的最新项目,其中有一个集合!
- 9. 如何反序列化具有子集合的集合?
- 10. 其中包含集合的类设计,其中的集合
- 11. 平面集合到一对多集合
- 12. 如何将集合添加到Backbone中的另一个集合
- 13. WPF TreeView HierarchicalDataTemplate - 绑定到具有不同子集合的对象
- 14. 其中集合
- 15. 查找对象集合中的数据,其中对象中的所有名称都具有相同的值
- 16. .net集合:如何将对象从一个集合复制到另一个集合?
- 17. 对象集合
- 18. 泛型集合 - 确保集合只包含一种类型的对象?
- 19. Ruby Ohm,如何在一个集合或集合中找到记录?
- 20. 合金约束如何将一个集合放入其子集中?
- 21. 如何仅在集合A中找到不同集合的网址
- 22. 查找集合的所有子集
- 23. 声明一个类型作为一个集合的子集
- 24. MDX:集合中的集合
- 25. 集合中的Hibernate集合
- 26. 集合与子集合
- 27. 如何使用MongoDB有效地将数据从一个集合集合到另一个集合中?
- 28. 如何在地图中有效地找到一组集合的子集?
- 29. 如何在Silverlight中将两个可观察的集合合并到集合中
- 30. C#如何用另一个集合中的对象替换一个集合中的对象
我认为这是一个最好的方法是提取一些接口并在T和T2类型中实现它。你能展示T或T2类的例子吗? –