小编Pra*_*ota的帖子

从数据表中删除与 List<string> 匹配的行

我有一个 DataTable,我想删除所有与 a 匹配的行List< string>,该怎么做?以下是我的代码,

public static DataTable GetSkills(List<Skill> EnteredSkills)
{
    DataTable dt = new DataTable();
    dt = GetDBMaster("SkillMaster");
    List<string> MatchingSkills = EnteredSkills.Select(c => c.Text).ToList();
    //Logic to Delete rows MatchingSkills from dt here
    return dt;
}
Run Code Online (Sandbox Code Playgroud)

最终解决方案

    public static DataTable GetSkills(List<Skill> EnteredSkills)
    {
        DataTable dt = new DataTable();
        dt = GetDBMaster("SkillMaster");
        var MatchingSkills = new HashSet<string>(EnteredSkills.Select(c => c.Text));
        List<DataRow> removeRows = dt.AsEnumerable().Where(r => MatchingSkills.Contains(r.Field<string>("DataTableSkillColumnName"))).ToList();
        removeRows.ForEach(dt.Rows.Remove);
        return dt;
    }
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net

4
推荐指数
1
解决办法
4469
查看次数

标签 统计

asp.net ×1

c# ×1

linq ×1