score:1

Accepted answer

as far as a1/a2 are the keys, no linq is required:

foeach(var a in new[] { "a1", "a2" })
    lw.items.removebykey(a);

why not?


but if you want to use linq at any cost, write you own extension method:

public static void foreach<t>(this ienumerable<t> collection, action<t> action)
{
    foreach (var item in collection)
        action(item);
}

and use it this way:

new[] { "a1", "a2" }.foreach(a => lw.removebykey(a));

however keep in mind that this is a well-known but disputable approach.

btw, list<t> already has such extension method, but my works for any ienumerable<t>:

new list<string> { "a1", "a2" }.foreach(); // evokes list<t>.foreach
new[] { "a1", "a2" }.foreach(); // evokes my
new list<string> { "a1", "a2" }.asenumerable().foreach(); // evokes my, i guess

score:0

to do this, you can make a list of excluded items (and populate them however you want, but in this case i'm choosing a compile time list. then you simply select all items in the first list and use the except method to exclude items from the other list.

list<string> lst = new list<string>{"a1", "a2", "a3" };
list<string> excludedlist = new list<string>{ "a1", "a2" };

var list = lst.select(e => e)
              .except(excludedlist);

foreach (var a in list)
{
    console.writeline(a + "\n"); //displays a3
}

Related Query

More Query from same tag