Accepted answer

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

foeach(var a in new[] { "a1", "a2" })

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)

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


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 = => e)

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

Related Query

More Query from same tag