我有一個對象列表List<Points>
,每個對象(點)都有一些屬性,如x,y和z。 (Points.x
,Points.y
,Points.z
是雙打) 我想刪除有一些條件的對象。例如,應刪除具有x < = 5,6 < = y < 10,z < 20的對象。我怎樣才能做到這一點?有條件地從列表中刪除項目
感謝
我有一個對象列表List<Points>
,每個對象(點)都有一些屬性,如x,y和z。 (Points.x
,Points.y
,Points.z
是雙打) 我想刪除有一些條件的對象。例如,應刪除具有x < = 5,6 < = y < 10,z < 20的對象。我怎樣才能做到這一點?有條件地從列表中刪除項目
感謝
您可以使用RemoveAll
方法是這樣的:
list.RemoveAll(p => p.x <= 5 && p.y >= 6 && p.y < 10 && p.z < 20);
RemoveAll列出了一個與Linq無關的方法 –
您是對的。 Ty –
您可以使用LINQ
var pointList = new List<Point>();
pointList = pointList.Where(p =>
!((p.X <= 5) && (p.Y > 6 && p.Y < 10) && (p.Z < 20)))
.ToList();
或列表對象
pointList.RemoveAll(p => (p.X <= 5) && (p.Y > 6 && p.Y < 10) && (p.Z < 20));
謝謝。 'List
p =>是Lambda表達式https://msdn.microsoft.com/zh-cn/library/bb397687.aspx ?f = 255&MSPPError = -2147217396 –
如果OpenPoints包含所有點對象,則RemoveAll將刪除所有匹配由指定謂詞定義的條件的對象.https://msdn.microsoft.com/en-us/library/wdka673a(v = vs.110).aspx @KatatoniaSh –
搜索上的另一個使用RemoveAll方法對於'LINQ To Obj ects',如果你的代碼不能按預期工作,請回來。 –
'points.RemoveAll(point => point.X <= 5 && point.Y <= 6 ...)' – sed