列表删除错误
Errors in List deleting
我已经创建了一个列表和一个二进制文件来存储数据。现在我尝试从列表中删除,但它不起作用,为什么?
错误显示:
Error 2 : Argument 1: cannot convert from 'XYZ_System.Log' to 'System.Predicate' E:\Degree Assignment\Application development-semester 1\XYZ_System\XYZ_System\RegisterUser.cs 239 32 XYZ_System
Error1 :The best overloaded method match for 'System.Collections.Generic.List.RemoveAll(System.Predicate)' has some invalid arguments E:\Degree Assignment\Application development-semester 1\XYZ_System\XYZ_System\RegisterUser.cs 239 17 XYZ_System
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
Log lg = new Log();
// lg.Username = this.textBox1.Text;
//lg.Password = this.textBox2.Text;
// lg.Name = this.txtname.Text;
// lg.Contact = Convert.ToInt32(this.txtContact_no.Text); ;
// lg.Email = this.txtEmail_Address.Text;
Stream stream = File.Open("Login.bin", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
list = (List<Log>)bformatter.Deserialize(stream);
stream.Close();
list.RemoveAll(lg);
// dtvregister.DataSource = list;
{
MessageBox.Show("Selected details has been deleted !", "Success");
Reset();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
list.RemoveAll()
需要一个函数 (Predicate<T>
),该函数 returns 一个布尔值,如果应删除该项目,则将针对每个项目调用该函数。
这是一个明确的例子:
private bool ValidateItem(Log lg)
{
if(lg.Name == "John")
return true;
else
return false;
}
list.RemoveAll(ValidateItem);
但是对于 lambda 表达式,这也是一样的:list.RemoveAll(lg => lg.Name == "John");
在您的情况下,可以使用:list.RemoveAll(lg => true);
,但您最好使用 list.Clear();
。
我已经创建了一个列表和一个二进制文件来存储数据。现在我尝试从列表中删除,但它不起作用,为什么?
错误显示:
Error 2 : Argument 1: cannot convert from 'XYZ_System.Log' to 'System.Predicate' E:\Degree Assignment\Application development-semester 1\XYZ_System\XYZ_System\RegisterUser.cs 239 32 XYZ_System
Error1 :The best overloaded method match for 'System.Collections.Generic.List.RemoveAll(System.Predicate)' has some invalid arguments E:\Degree Assignment\Application development-semester 1\XYZ_System\XYZ_System\RegisterUser.cs 239 17 XYZ_System
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
Log lg = new Log();
// lg.Username = this.textBox1.Text;
//lg.Password = this.textBox2.Text;
// lg.Name = this.txtname.Text;
// lg.Contact = Convert.ToInt32(this.txtContact_no.Text); ;
// lg.Email = this.txtEmail_Address.Text;
Stream stream = File.Open("Login.bin", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
list = (List<Log>)bformatter.Deserialize(stream);
stream.Close();
list.RemoveAll(lg);
// dtvregister.DataSource = list;
{
MessageBox.Show("Selected details has been deleted !", "Success");
Reset();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
list.RemoveAll()
需要一个函数 (Predicate<T>
),该函数 returns 一个布尔值,如果应删除该项目,则将针对每个项目调用该函数。
这是一个明确的例子:
private bool ValidateItem(Log lg)
{
if(lg.Name == "John")
return true;
else
return false;
}
list.RemoveAll(ValidateItem);
但是对于 lambda 表达式,这也是一样的:list.RemoveAll(lg => lg.Name == "John");
在您的情况下,可以使用:list.RemoveAll(lg => true);
,但您最好使用 list.Clear();
。