我有下一种情况:
public class ParentClass
{
public string Name { get; set; }
public List<ChildClass> ChildrenList {get;set;}
}
public class ChildClass
{
public string Name { get; set; }
public GrandChildClass GrandChild { get; set; }
}
public class GrandChildClass
{
public string Name { get; set; }
}
GrandChildClass grandChild1 = new GrandChildClass() { Name = "AAA1" };
GrandChildClass grandChild2 = new GrandChildClass() { Name = "BBB1" };
GrandChildClass grandChild3 = new GrandChildClass() { Name = "CCC1" };
GrandChildClass grandChild4 = new GrandChildClass() { Name = "DDD1" };
ChildClass child1 = new ChildClass() { Name = "AAA", GrandChild = grandChild1 };
ChildClass child2 = new ChildClass() { Name = "BBB", GrandChild = grandChild2 };
ChildClass child3 = new ChildClass() { Name = "CCC", GrandChild = grandChild3 };
ChildClass child4 = new ChildClass() { Name = "DDD", GrandChild = grandChild4 };
ChildClass child5 = new ChildClass() { Name = "EEE", GrandChild = grandChild2 };
List<ParentClass> parentsList = new List<ParentClass>()
{
new ParentClass { Name = "Parent1", ChildrenList = new List<ChildClass>() { child1, child2 } },
new ParentClass { Name = "Parent2", ChildrenList = new List<ChildClass>() { child3, child4 } },
new ParentClass { Name = "Parent3", ChildrenList = new List<ChildClass>() { child1, child5 } }
}
我想以最简单的方式找出哪些父母有共同的子女。
我不想做一些foreach循环并进行类似的比较,但是我想使用linq函数,例如Intersect或类似的函数。
例如,这是一个类似的东西,正在起作用:
List<List<string>> lists = new List<List<string>>()
{
new List<string> {"Hello", "World", "7"},
new List<string> {"Hello", "7", "Person"},
new List<string> {"7", "7", "Hello"}
};
List<string> extList = lists.Cast<IEnumerable<string>>()
.Aggregate((a, b) => a.Intersect(b)).ToList();
就我而言,我希望结果是: Parent1和Parent3有共同的child1。
有什么主意吗?
编辑:
此外,如何查找所有有共同孙子的父母?
答案 0 :(得分:2)
您正在寻找类似的东西:
var results = parentsList
.SelectMany(parent => parent.ChildrenList)
.Distinct()
.Select(child => new
{
Child = child,
Parents = parentsList.Where(parent => parent.ChildrenList.Contains(child))
});
您需要展平ChildrenLists并区分它们。接下来,您应该选择一个孩子,以及在其ChildrenList中拥有这个孩子的所有父母。
为了更有用的结果,我已经命名了父母:
AAA => X, Z
BBB => X
CCC => Y
DDD => Y
EEE => Z
就我而言,它说,这个孩子有这些父母。
答案 1 :(得分:1)
您可以按孩子分组,然后选择有多个父母的分组:
var result = parentsList.SelectMany(p => p.ChildrenList.Select(c => (child: c, parent: p)))
.GroupBy(c => c.child, c => c.parent)
.Where(g => g.Count() > 1);
我已经修改了您的输入并将Name
属性添加到Parent类:
List<ParentClass> parentsList = new List<ParentClass>()
{
new ParentClass { Name = "p1", ChildrenList = new List<ChildClass>() { child1, child2 } },
new ParentClass { Name = "p2", ChildrenList = new List<ChildClass>() { child3, child4 } },
new ParentClass { Name = "p3", ChildrenList = new List<ChildClass>() { child1, child5 } }
};
下一个代码
foreach (var child in result)
Console.WriteLine(String.Join(", ", child.Select(p => p.Name)));
将打印
p1,p3
答案 2 :(得分:0)
您可以使用下一个代码找到所有具有相同引用的子代:
List<ChildClass> result = parentsList.SelectMany(x => x.ChildrenList)
.GroupBy(x => x).Where( x=> x.Count() > 1)
.SelectMany(x => x).Distinct().ToList() ;
顺便说一句,由于每个子列表中都遇到“ Hello”,因此您的代码正在工作,请在第二个列表中跳过它,结果列表将为空:
列表extList = list.Cast>() .Aggregate(((a,b)=> a.Intersect(b))。ToList();
答案 3 :(得分:0)
我要为父母寻找共同的孩子:
var results = parentsList
.SelectMany(p => p.ChildrenList)
.Distinct()
.Select(child => new
{
Child = child,
Parents = parentsList.Where(p => p.ChildrenList.Contains(child)).ToList()
}).ToList();
我要为父母寻找共同的孙子女:
var results = parentsList
.SelectMany(p => p.ChildrenList)
.Distinct()
.Select(child => new
{
child.GrandChild,
Parent = parentsList.Where(p => p.ChildrenList.Any(c => c.GrandChild == child.GrandChild)).ToList()
}).GroupBy(c => c.GrandChild)
.Select(group => group.First());
感谢Jeroen van Langen的回答。