generics - C# List<object>.RemoveAll() - How to remove a subset of the list? -
i have 2 classes feeds_auto , product multiple matching properties. particular problem, autoid field need use.
i have list<feedsauto>
several hundred unique entries. have small list<product>
10, 20 unique entries. want remove items in small list large list.
how use removeall({lambda expression}) accomplish this? of examples have found depend on generic list being of simple types (strings, ints, etc).
private static int doinserts(ref list<model.feeds_auto> source, ref list<model.product> target, guid companyid) { list<model.product> newproductlist = new list<model.product>(); list<model.product> dropsourcelist = new list<model.product>(); using (var db = helpers.getproddb()) { foreach (var src in source) { var tgt = target.where(a => a.alternateproductid == src.autoid && a.lastfeedupdate < src.datemodified).firstordefault(); if (tgt == null) { newproductlist.add(new model.product{...}); dropsourcelist.add(src); } } db.savechanges(); if (dropsourcelist.count > 0) { source.removeall(????); } } }
to in loop not difficult:
foreach (var drop in dropsourcelist) { source.removeall(a => a.autoid == drop.autoid); }
it not make sense (to me) loop on set call removeall 1 item in each pass.
you can use any
simplify
source.removeall(a => dropsourcelist.any(b => a.autoid == b.autoid));
you can reduce looping creating hashset
of id's first:
var toremove = new hashset<int>(dropsourcelist.convertall(a => a.autoid)); source.removeall(a => toremove.contains(a.autoid));
Comments
Post a Comment