簡體   English   中英

C#在多個對象列表之間相交

[英]C# Intersect beetween multiple objects lists

我有下一個情況:

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。

任何的想法?

編輯:

另外,如何找到所有有共同孫子的父母?

您正在尋找類似的東西:

var results = parentsList
  .SelectMany(parent => parent.ChildrenList)
  .Distinct()
  .Select(child => new 
          { 
              Child = child, 
              Parents = parentsList.Where(parent => parent.ChildrenList.Contains(child)) 
          });

你需要壓扁ChildrenLists並區分它們。 接下來,您應該選擇子項以及在ChildList中擁有此子項的所有父項。

我已經為父母命名了更有用的結果:

AAA => X, Z
BBB => X
CCC => Y
DDD => Y
EEE => Z

在我的情況下它說,這個孩子有這些父母。

您可以按子組進行分組,然后選擇包含多個父組的組:

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

您可以使用下一個代碼找到所有具有相同參考的孩子:

List<ChildClass> result = parentsList.SelectMany(x => x.ChildrenList)
.GroupBy(x => x).Where( x=> x.Count() > 1)
.SelectMany(x => x).Distinct().ToList() ;

BTW您的代碼正在運行,因為在每個子列表中遇到“Hello”,在第二個列表中跳過它,結果List將為空:

列表extList = lists.Cast>()。Aggregate((a,b)=> a.Intersect(b))。ToList();

為了找到父母的共同孩子,我使用:

var results = parentsList
    .SelectMany(p => p.ChildrenList)
    .Distinct()
    .Select(child => new
    {
        Child = child,
        Parents = parentsList.Where(p => p.ChildrenList.Contains(child)).ToList()
    }).ToList();

為了找到父母的共同GrandChildren,我使用:

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的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM