c# - indexof array using linq except -
c# - indexof array using linq except -
i have next 2 lists
list<string> list1 = new list<string>(); list1.add("one"); list1.add("two"); list1.add("three"); list1.add("four"); list1.add("five"); list<string> list2= new list<string>(); list2.add("one"); list2.add("two"); list2.add("three"); ienumerable<int> indexofunwantedcolumns; indexofunwantedcolumns = list1.except(list2) .select((p, i) => new { item = p, index = }) .select(p => p.index);
as expected give index of 2 items in new list (0 , 1). want able there index values original list (3 , 4)
any help appreciated
the main thought capture original index before filtering, , check if list2
contains each item. able perform these lookups efficiently (in o(1)
aggregated time per lookup), should utilize hashset:
var hashset = new hashset<string>(list2); var unwantedcolindexes = list1 .select((item, idx) => new { item, idx }) // capture original index .where(i => !hashset.contains(i.item)) // filter using hashset .select(i => i.idx) // index .tolist();
c# .net linq
Comments
Post a Comment