簡體   English   中英

C#Linq - 給出兩個列表,如何判斷它們中是否包含其中一個

[英]C# Linq - Given two lists, how do I tell if either of them are contained in each other

我有兩個清單:

ListA:

"Brown"
"Green"
"Yellow"
"Orange"

ListB:

"Yellow"
"Orange"

如果ListA或ListB彼此包含,我想返回true。 在這種情況下,ListB是ListA的一部分。 ListB不一定是小的。

我能想到的唯一解決方案是雙方都做Union,即首先使用ListB進行ListA,然后使用ListA進行ListB,當且僅當兩者都為真時返回true。

這是最好的方法,還是其他方式嗎?

我希望有一個Linq解決方案

你可以嘗試:

int count = ListA.Intersect(ListB).Count();
if ((count == ListA.Count()) || (count == ListB.Count())) {
    // One list contains other
}

檢查a.Except(b)b.Except(a)是否為空。

您可以使用intersect方法並檢查結果集是否與任一集相同。

暫無
暫無

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

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