IList 컬렉션에서 N 개의 항목을 제거하고 싶습니다. 다음은 내가 가지고있는 것입니다.술어와 일치하는 N 항목을 IList에서 제거합니다.
public void RemoveSubcomponentsByTemplate(int templateID, int countToRemove)
{
// TaskDeviceSubcomponents is an IList
var subcomponents = TaskDeviceSubcomponents.Where(tds => tds.TemplateID == templateID).ToList();
if (subcomponents.Count < countToRemove)
{
string message = string.Format("Attempted to remove more subcomponents than found. Found: {0}, attempted: {1}", subcomponents.Count, countToRemove);
throw new ApplicationException(message);
}
subcomponents.RemoveRange(0, countToRemove);
}
불행히도이 코드는 광고 한대로 작동하지 않습니다. TaskDeviceSubcomponents는 IList이므로 RemoveRange 메서드가 없습니다. 그래서, .ToList()를 호출하여 실제 List를 인스턴스화하지만, 이는 동일한 컬렉션 항목에 대한 참조가있는 중복 컬렉션을 제공합니다. 하위 구성 요소에서 RemoveRange를 호출해도 TaskDeviceSubcomponents에 영향을주지 않으므로 좋지 않습니다.
간단한 방법이 있나요? 나는 단지 그것을 보지 않고있다.
오 감사합니다! .Take()는 실제로 도움이되었습니다. :) –