我有一堂课如下
public class Material
{
public string `MaterialName` { get; set; }
public List<Material> `ChildMaterial` { get; set; }
}我使用上面的类创建了一个嵌套列表,如下例所示
- **Material D**
- Material F
我想要一个linq查询的一些方法,将过滤掉材料D,并将给我的路径,直到根,并将删除它下面的所有节点。如下面的例子所示,这个材料D可以在树中的任何级别上找到,并且可以重复。
发布于 2015-09-15 08:57:19
首先,您需要一个LINQ方法,它能够将这样的树结构夷为平地,以便能够迭代所有可用的节点:
public static IEnumerable<TSource> Map<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> selectorFunction,
Func<TSource, IEnumerable<TSource>> getChildrenFunction)
{
if (source == null)
throw new ArgumentNullException("source");
if (selectorFunction == null)
throw new ArgumentNullException("selectorFunction");
if (getChildrenFunction == null)
throw new ArgumentNullException("getChildrenFunction");
return MapImpl(source, selectorFunction, getChildrenFunction);
}
private static IEnumerable<TSource> MapImpl<TSource>(
IEnumerable<TSource> source,
Func<TSource, bool> selectorFunction,
Func<TSource, IEnumerable<TSource>> getChildrenFunction)
{
// Go through the input enumerable looking for children,
// and add those if we have them
foreach (TSource element in source)
{
foreach (var childElement in MapImpl(getChildrenFunction(element), selectorFunction, getChildrenFunction))
{
yield return childElement;
}
if (selectorFunction(element))
yield return element;
}
}这样,您现在就可以在主列表中运行,并找到所有想要的节点:
var matchingMaterials = MyMaterials.Map(material => true, material => material, material => material.ChildMeterials)
.Where(material => material.Name = "Material D")
// Materialization is needed, cause manipulation is desired while iterating over the result.
.ToList();然后您喜欢以某种方式操作这些节点(例如,从匹配的节点中删除所有子节点):
foreach(var material in matchingMaterials)
{
material.ChildMaterials.Clear();
}https://stackoverflow.com/questions/32578907
复制相似问题