Lambda syntax to remove rows from List
Given:
string removeRows = "";
int i = 0;
foreach (var row in userStats)
开发者_运维技巧{
if (row.OrderRow.RegistrationType == "Want Removed")
{
removeRows = removeRows + i.ToString() + ",";
}
i++;
}
what's the Lambda syntax to execute the removal?
As I understand, you code collects the indexes of rows to be removed and forms a string with them comma-separated. It doesn't actually removes these items so I don't know why you're doing it.
If userStats
is List<T>
where T
is your row type, you can use RemoveAll
method that actually removes all items that match given condition:
userStats.RemoveAll (r => r.OrderRow.RegistrationType == "Want Removed");
I think your code abuses strings, you both use them for collecting indexes and for registration type. I wonder if RegistrationType
could be an enumeration instead:
enum RegitrationType {
ShouldBeRemoved,
// add other types here
}
userStats.RemoveAll (r =>
r.OrderRow.RegistrationType == RegitrationType.ShouldBeRemoved);
精彩评论