C# 如何从.net中的数据表中删除特定值
我想从C# 如何从.net中的数据表中删除特定值,c#,.net,linq,datatable,C#,.net,Linq,Datatable,我想从数据表中删除一个特定的值,不像特定的行或列,假设我有如下所示的数据表 ====================================================== | firstname | lastname | age | location | ====================================================== | Jhonson | charles | 32 | dever
数据表
中删除一个特定的值,不像特定的行或列,假设我有如下所示的数据表
======================================================
| firstname | lastname | age | location |
======================================================
| Jhonson | charles | 32 | dever |
| Jaques | kallis | 33 | cevek |
| rama | kalyani | 23 | qwet |
| sasi | kiran | 22 | delli |
======================================================
我有一个名为
age.txt
的文件,其中包含值23、26、27。因此,如果datatable列age
中的三个值中有任何一个值,只需清除该值,而不清除特定的行或列。我如何实现这一点?请参阅答案。然后只需检查插入的值,并根据需要进行更改。请参阅答案。然后只需检查插入的值并根据需要进行更改。您可以迭代数据表中的行并将值设置为null
// Split the text values into an array of strings
string[] checkAgeArray = "23,26,27".Split(',');
DataTable person;
foreach (var row in person.Rows)
{
// Compare the string ages to the string value of the integer age in table
if (checkAgeArray.Contains(row["age"].ToString()))
row["age"] = System.DBNull;
}
您可以迭代datatable中的行,并将值设置为null
// Split the text values into an array of strings
string[] checkAgeArray = "23,26,27".Split(',');
DataTable person;
foreach (var row in person.Rows)
{
// Compare the string ages to the string value of the integer age in table
if (checkAgeArray.Contains(row["age"].ToString()))
row["age"] = System.DBNull;
}
我假设您的文件在一行文本中包含所有年龄,并且您的年龄字段是整数类型
DataTable dt = LoadYourTable();
string agesData = File.ReadAllText("age.txt");
// Create an array of the ages integer loaded from your file
int[] ages = agesData.Split(',').Select(x => Convert.ToInt32(x)).ToArray();
// Loop over the rows collection
foreach(DataRow r in dt.Rows)
{
// If the ages array contains the Age value of the current row
if(ages.Contains(r.Field<int>("Age")))
r.SetField<int>("Age", 0); // Or set it to DBNull.Value
}
DataTable dt=LoadYourTable();
字符串agesData=File.ReadAllText(“age.txt”);
//创建一个从文件加载的整数数组
int[]ages=agesData.Split(',')。选择(x=>Convert.ToInt32(x)).ToArray();
//在行集合上循环
foreach(数据行r在dt.行中)
{
//如果ages数组包含当前行的Age值
if(ages.Contains(r.Field(“Age”)))
r、 SetField(“Age”,0);//或将其设置为DBNull.Value
}
我假设您的文件在一行文本中包含所有年龄,并且您的年龄字段为整数类型
DataTable dt = LoadYourTable();
string agesData = File.ReadAllText("age.txt");
// Create an array of the ages integer loaded from your file
int[] ages = agesData.Split(',').Select(x => Convert.ToInt32(x)).ToArray();
// Loop over the rows collection
foreach(DataRow r in dt.Rows)
{
// If the ages array contains the Age value of the current row
if(ages.Contains(r.Field<int>("Age")))
r.SetField<int>("Age", 0); // Or set it to DBNull.Value
}
DataTable dt=LoadYourTable();
字符串agesData=File.ReadAllText(“age.txt”);
//创建一个从文件加载的整数数组
int[]ages=agesData.Split(',')。选择(x=>Convert.ToInt32(x)).ToArray();
//在行集合上循环
foreach(数据行r在dt.行中)
{
//如果ages数组包含当前行的Age值
if(ages.Contains(r.Field(“Age”)))
r、 SetField(“Age”,0);//或将其设置为DBNull.Value
}
这些年龄值都在一行逗号分隔的行中或在单独的行中?这些年龄值都在一行逗号分隔的行中或在单独的行中?获取错误“无法将索引应用于[]表达式类型的对象”为什么?获取错误“无法将索引应用于[]表达式类型的对象”为什么?