Linq检查列表

时间:2017-06-22 11:50:24

标签: c# linq

我有一个名为OfferPriorities的对象列表,OfferPriority有一个字段TypeId。我想确保OfferPriorities中存在所有TypeId值。可能的TypeId值是1,2和3.我想检查OfferPriorities中是否存在所有这些值。例如,下面TypeId 3在列表中不存在,所以我应该抛出异常。

Id | TYPEID

5 | 1

6 | 2

2 个答案:

答案 0 :(得分:3)

<TextView android:id="@+id/myTextView" ... (rest of lines) app:layout_constraintVertical_bias="0.22000003" /> + Except做你想做的事:

Any

答案 1 :(得分:0)

另一种可能是使用All来检查OfferPriorities是否包含所有元素:

List<OfferPriority> OfferPriorities = new List<OfferPriority>();

OfferPriorities.Add(new OfferPriority() { TypeId = 1 });
OfferPriorities.Add(new OfferPriority() { TypeId = 2 });
//OfferPriorities.Add(new OfferPriority() { TypeId = 3 });

List<int> allowedIDs = new List<int> { 1, 2, 3 };

bool check = allowedIDs.All(x => OfferPriorities.Select(y => y.TypeId).Contains(x));
相关问题