Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查对象列表是否包含特定值_C#_List_Object_Contains - Fatal编程技术网

C# 检查对象列表是否包含特定值

C# 检查对象列表是否包含特定值,c#,list,object,contains,C#,List,Object,Contains,如果可能的话,我试图找出如何检查对象是否包含特定值 我的对象内容列表如下: [{ "TypeId": 1, "Content": "Some content here" }, { "TypeId": 2, "Content": "Some new content here" }, { "TypeId": 4, "Content": "Some other content here" }] 现在,我希望能够做的是搜索如下内容: if(comment

如果可能的话,我试图找出如何检查对象是否包含特定值

我的对象内容列表如下:

[{
    "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的对象

这能做到吗

感谢您的帮助:-

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

commentsList.Any(x => x.TypeId == 4)

commentsList.Selectx=>x.TypeId.Contains4?或者正如Jerodev的回答所说,commentsList.Anyx=>x.TypeId??4查看您的数据,由TypeId键入的字典可能是比列表更好的容器,您可以使用dic.ContainsKey4进行最佳检索。@vc74但您不需要将对象拆分为两个不同的对象,一个是TypeId类型,另一个是内容类型?@MindSwipe否,这将是一个Dictionary@vc74你可以这么做。但现在你们有一张你们需要撕开的清单。我明白你对字典的看法,但字典只有在你拥有JSON序列化数据并需要将其反序列化的情况下才有意义。实现这一点的方法很多,使用任何一种都是最干净的方法之一