I have the following ListView:

and I'm creating a List<string> of the items I want a list of rows to remove from that ListView:
List<String> lst =new List<String>{"A1","A2"};
I know that I can delete the rows using iterating through the index and deleting the item using RemoveAt()开发者_开发问答 function, but is there any way to do this using LINQ?
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
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
}
加载中,请稍侯......
精彩评论