检查对象列表是否包含特定值
Check if list of objects contains a specific value
如果可能的话,我正在尝试找出如何检查对象是否包含特定值。
我的对象内容列表如下:
[{
"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 的对象。
这可以做到吗?
感谢任何帮助并提前致谢:-)
使用Any检查列表中是否有满足要求的元素。
commentsList.Any(x => x.TypeId == 4)
如果可能的话,我正在尝试找出如何检查对象是否包含特定值。
我的对象内容列表如下:
[{
"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 的对象。
这可以做到吗?
感谢任何帮助并提前致谢:-)
使用Any检查列表中是否有满足要求的元素。
commentsList.Any(x => x.TypeId == 4)