我有一个DataTable,我想删除与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;
}
最终解决方案
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;
}
答案 0 :(得分:4)
假设该列为SkillName
List<DataRow> removeRows = dt.AsEnumerable()
.Where(r => MatchingSkills.Contains(r.Field<string>("SkillName")))
.ToList();
removeRows.ForEach(dt.Rows.Remove);
旁注:我会使用HashSet<string>
因为效率更高:
var MatchingSkills = new HashSet<string>(EnteredSkills.Select(c => c.Text));
答案 1 :(得分:1)
最简单的方法是:
var lstRemoveColumns = new List<string>() { "ColValue1", "ColVal2", "ColValue3", "ColValue4" };
List<DataRow> rowsToDelete = new List<DataRow>();
foreach (DataRow row in dt.Rows)
{
if (lstRemoveColumns.Contains(row["ColumnName"].ToString()))
{
rowsToDelete.Add(row);
}
}
foreach (DataRow row in rowsToDelete)
{
dt.Rows.Remove(row);
}
dt.AcceptChanges();
看here
答案 2 :(得分:0)
此解决方案效果很好!我将其用于解决方案。
html,
body {
margin: 0;
}
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex-grow: 1;
display: flex;
background-color: #eee;
}
.panel1,
.panel2 {
background-color: brown;
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
}
.panel-content {
background-color: #ccc;
padding: 3em;
}
header,
footer {
background-color: #ccc;
padding: 1em;
text-align: center;
}