开发者

Lambda syntax to remove rows from List

开发者 https://www.devze.com 2023-02-08 23:12 出处:网络
Given: string removeRows = \"\"; int i = 0; foreach (var row in userStats) 开发者_运维技巧{ if (row.OrderRow.RegistrationType == \"Want Removed\")

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消