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

Pra*_*ota 4 c# linq asp.net

我有一个 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)

Tim*_*ter 5

假设该列是SkillName

List<DataRow> removeRows = dt.AsEnumerable()
    .Where(r => MatchingSkills.Contains(r.Field<string>("SkillName")))
    .ToList();
removeRows.ForEach(dt.Rows.Remove);
Run Code Online (Sandbox Code Playgroud)

旁注:我会使用 aHashSet<string>因为它会更有效:

var MatchingSkills = new HashSet<string>(EnteredSkills.Select(c => c.Text));
Run Code Online (Sandbox Code Playgroud)