3 回答

TA貢獻1817條經驗 獲得超14個贊
如果您使用的是.NET 3.5,則很簡單:
public class ListHelper<T>
{
public static bool ContainsAllItems(List<T> a, List<T> b)
{
return !b.Except(a).Any();
}
}
這個檢查是否有任何元件在b其不在a-然后反轉的結果。
請注意,使該方法泛型而不是使類更傳統,并且沒有理由要求List<T>代替IEnumerable<T>-因此,這可能是更可取的:
public static class LinqExtras // Or whatever
{
public static bool ContainsAllItems<T>(this IEnumerable<T> a, IEnumerable<T> b)
{
return !b.Except(a).Any();
}
}

TA貢獻1765條經驗 獲得超5個贊
您也可以使用其他方式。覆蓋等于并使用它
public bool ContainsAll(List<T> a,List<T> check)
{
list l = new List<T>(check);
foreach(T _t in a)
{
if(check.Contains(t))
{
check.Remove(t);
if(check.Count == 0)
{
return true;
}
}
return false;
}
}
- 3 回答
- 0 關注
- 874 瀏覽
添加回答
舉報