-3

如果可能的话,我试图弄清楚如何检查对象是否包含特定值。

我的对象内容列表如下:

[{
    "TypeId": 1,
    "Content": "Some content here"
},
{
    "TypeId": 2,
    "Content": "Some new content here"
},
{
    "TypeId": 4,
    "Content": "Some other content here"
}]

现在,我想做的是搜索这样的东西:

if(commentsList.Contains(4))

我想检查 commentsList 是否有一个 TypeId 为 4 的对象。

这可以做到吗?

任何帮助表示赞赏并提前感谢:-)

4

1 回答 1

3

使用Any检查列表中是否有满足要求的元素。

commentsList.Any(x => x.TypeId == 4)
于 2019-02-11T10:42:08.340 回答