Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 - Fatal编程技术网

C# 检查列表是否已包含项目?

C# 检查列表是否已包含项目?,c#,list,C#,List,我用了一个struct public struct stuff { public int ID; public int quan; } 在List stuff=new List()中 如何检查列表中已有的内容“where ID=1”?您可以非常轻松地使用LINQ bool res = stuff.Any(c => c.ID == 1); doh,那是两个字母。。。1次迭代,如stuff.Any(s=>s.ID==1)会更好:)@andreas niedrmair只有一次

我用了一个struct

public struct stuff
{
    public int ID;
    public int quan;
}
List stuff=new List()中


如何检查列表中已有的内容“where ID=1”?

您可以非常轻松地使用LINQ

bool res = stuff.Any(c => c.ID == 1);

doh,那是两个字母。。。1次迭代,如
stuff.Any(s=>s.ID==1)会更好:)@andreas niedrmair只有一次迭代,但是当在列表中找到元素时,更深层次的函数调用Overedit返回true。
bool isContains = stuff.Any(x => x.ID == 1);
if(stuf.Select(x => x.id).Contains(1))
{
    //Do Stuff
}