我有一个这样的 struct :
public struct stuff
{
public int ID;
public int quan;
}
and want to to remove the product where ID
is 1.
I'm trying this currently:
prods.Remove(new stuff{ prodID = 1});
但它不起作用.
THANKS TO ALL
我有一个这样的 struct :
public struct stuff
{
public int ID;
public int quan;
}
and want to to remove the product where ID
is 1.
I'm trying this currently:
prods.Remove(new stuff{ prodID = 1});
但它不起作用.
THANKS TO ALL
使用LINQ:
prods.Remove( prods.Single( s => s.ID == 1 ) );
也许你甚至想用SingleOrDefault()
来判断元素是否存在...
EDIT:
Since stuff
is a struct, SingleOrDefault()
will not return null. But it will return default( stuff ), which will have an ID of 0. When you don't have an ID of 0 for your normal stuff-objects you can query for this ID:
var stuffToRemove = prods.SingleOrDefault( s => s.ID == 1 );
if( stuffToRemove.ID != 0 )
{
prods.Remove( stuffToRemove );
}