如果我有一個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. 集合的通用類型子集合
- 2. 如何從另一個集合中獲取對象集合?
- 3. LINQ:在一個集合中搜索單個對象的集合
- 4. 如何從集合列表中找到最大集合或超集合(最大集合不是集合中列表中的另一集合的集合)
- 5. 可能在集合中有子集合?
- 6. 集合集合中的唯一集合
- 7. 從集合ArrayList中返回一個元素,其中每個集合具有不同的元素類型
- 8. 問題:如何驗證對象集合中的對象集合
- 9. 添加具有子集合的對象
- 10. 如何在Scala中創建一個類型對象的集合
- 11. 在對集合執行多個操作後返回相同的集合類型
- 12. Backbone中的集合和子集合(及其相關視圖)
- 13. 如何在另一個集合中獲取具有此集合的集合和一個元素的實體
- 14. MongoDB集合可以在其中有另一個集合嗎?
- 15. 從泛型集合中選擇一個類型的子集
- 16. 集合中的一個集合
- 17. 如何反序列化具有子集合的集合?
- 18. LINQ查找集合中的最新項目,其中有一個集合!
- 19. 平面集合到一對多集合
- 20. 其中包含集合的類設計,其中的集合
- 21. Java:將某些類型的集合中的對象包含在集合中以存儲重複集合
- 22. 如何將集合添加到Backbone中的另一個集合
- 23. 如何找到一個具有顯示屬性集的div集合中的div
- 24. 骨幹集合 - 如何過濾與其他集合的集合?
- 25. WPF TreeView HierarchicalDataTemplate - 綁定到具有不同子集合的對象
- 26. 如何在集合中添加集合?
- 27. 如何綁定到集合集合下的對象的屬性?
- 28. 平均集合內的對象和治療等同一集合
- 29. 其中集合
- 30. 查找對象集合中的數據,其中對象中的所有名稱都具有相同的值
我認爲這是一個最好的方法是提取一些接口並在T和T2類型中實現它。你能展示T或T2類的例子嗎? –