Если VList<T>
является List<T>
, то вы можете сделать это:
profileList.RemoveAll(profile => GuidList.Contains(profile.UserId));
Если производительность является проблемой, и для удаления есть МНОЖЕСТВО GuidsList, вы можете сделать GuidList HashSet<Guid>
.
Редактировать На основе комментариев: Если вы не хотите изменять исходный список, сделайте следующее:
var filtered = new VList<VW_profiles>(
profileList.Where(profile => !GuidList.Contains(profile.UserId)));
Редактировать Если вы не используете List<T>
, вот метод, который вы можете использовать в списках с изменяемым размером, реализующий IList<T>
, и метод, который вы можете использовать в массивах (T[]
). Удаляя только элементы из конца списка, алгоритм O (n²) будет иметь значение O (n) для большинства реализаций IList<T>
.
.
public static void RemoveAll<T>(this IList<T> list, Predicate<T> match)
{
if (list == null)
throw new ArgumentNullException("list");
if (match == null)
throw new ArgumentNullException("match");
if (list is T[])
throw new ArgumentException("Arrays cannot be resized.");
// early out
if (list.Count == 0)
return;
// List<T> provides special handling
List<T> genericList = list as List<T>;
if (genericList != null)
{
genericList.RemoveAll(match);
return;
}
int targetIndex = 0;
for (int i = 0; i < list.Count; i++)
{
if (!match(list[i]) && targetIndex != i)
{
list[targetIndex] = list[i];
targetIndex++;
}
}
// Unfortunately IList<T> doesn't have RemoveRange either
for (int i = list.Count - 1; i >= targetIndex; i--)
{
list.RemoveAt(i);
}
}
public static void RemoveAll<T>(ref T[] array, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException("array");
if (match == null)
throw new ArgumentNullException("match");
int targetIndex = 0;
for (int i = 0; i < array.Length; i++)
{
if (!match(array[i]) && targetIndex != i)
{
array[targetIndex] = array[i];
targetIndex++;
}
}
if (targetIndex != array.Length)
{
Array.Resize(ref array, targetIndex);
}
}