C#在多个对象列表之间相交
我有下一个情况:C#在多个对象列表之间相交,c#,list,intersection,C#,List,Intersection,我有下一个情况: 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 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 } }
}
公共类父类
{
公共字符串名称{get;set;}
公共列表子列表{get;set;}
}
公营儿童班
{
公共字符串名称{get;set;}
公共类孙子{get;set;}
}
公共课
{
公共字符串名称{get;set;}
}
孙子类孙子1=新孙子类(){Name=“AAA1”};
孙子类孙子2=新孙子类(){Name=“BBB1”};
孙子类孙子3=新孙子类(){Name=“CCC1”};
孙子类孙子4=新孙子类(){Name=“DDD1”};
ChildClass child1=新的ChildClass(){Name=“AAA”,孙子=孙子1};
ChildClass child2=新的ChildClass(){Name=“BBB”,孙子=孙子2};
ChildClass child3=新的ChildClass(){Name=“CCC”,孙子=孙子3};
ChildClass child4=新的ChildClass(){Name=“DDD”,孙子=孙子4};
ChildClass child5=新的ChildClass(){Name=“EEE”,孙子=孙子2};
列表父列表=新列表()
{
新父类{Name=“Parent1”,ChildrenList=new List(){child1,child2},
新父类{Name=“Parent2”,ChildrenList=new List(){child3,child4},
新父类{Name=“Parent3”,ChildrenList=new List(){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();
列表列表=新列表()
{
新名单{“你好”,“世界”,“7”},
新列表{“你好”,“7”,“人”},
新列表{“7”、“7”、“你好”}
};
List extList=lists.Cast()
.Aggregate((a,b)=>a.Intersect(b)).ToList();
就我而言,我希望结果是:
Parent1和Parent3有共同的孩子1
有什么想法吗
编辑:
此外,如何找到所有有共同孙子女的父母?您正在寻找类似以下内容:
var results = parentsList
.SelectMany(parent => parent.ChildrenList)
.Distinct()
.Select(child => new
{
Child = child,
Parents = parentsList.Where(parent => parent.ChildrenList.Contains(child))
});
您需要将儿童列表展平并进行区分。接下来,您应该选择该孩子以及在其孩子列表中包含该孩子的所有家长
为了获得更有用的结果,我给父母命名:
AAA => X, Z
BBB => X
CCC => Y
DDD => Y
EEE => Z
在我的例子中,它说,这个孩子有这些父母。你可以用下一个代码找到具有相同引用的所有孩子:
List<ChildClass> result = parentsList.SelectMany(x => x.ChildrenList)
.GroupBy(x => x).Where( x=> x.Count() > 1)
.SelectMany(x => x).Distinct().ToList() ;
List result=parentsList.SelectMany(x=>x.ChildrenList)
.GroupBy(x=>x).Where(x=>x.Count()>1)
.SelectMany(x=>x.Distinct().ToList();
顺便说一句,您的代码正在工作,因为在每个子列表中遇到“Hello”,在第二个列表中跳过它,结果列表将为空:
List extList=lists.Cast>()
.Aggregate((a,b)=>a.Intersect(b)).ToList()
您可以按子级分组,然后选择具有多个父级的组:
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
属性添加到父类:
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 } }
};
将打印
p1,p3
要为父母找到共同的孩子,我使用:
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());
要为父母找到共同的孙子孙女,我使用:
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的回答。澄清一下:如果孩子共享一个
名称,而不是同一个对象实例?同一个对象实例,则认为孩子是平等的。