1ft*_*tw1 1 c# visual-studio-2010 visual-studio c#-3.0 c#-4.0
嗨,我有一个对象列表,我需要找出我的ID是否已经在列表中.在对象类中,我设置了id,我只想知道在Ui中输入的那个是否已经在使用中.
班级
class Product
{
private string name = "";
private int id = 0;
private decimal Pvalue = 0.00m;
private static int lastId = 1;
public Product(string namep, decimal valuep)
{
this.name = namep;
this.id = lastId++;
this.Pvalue = valuep;
}
public override string ToString()
{
return name + " " + id + " "+ Pvalue;
}
public bool Equals(Product p)
{
return (p.id == this.id);
}
}
Run Code Online (Sandbox Code Playgroud)
我试图解决它:
int id;
bool test = int.TryParse(textBoxId.Text, out id);
if(test)
{
if(Inv.Contains(id))
{
label2.Text = "you already have this id";
}else
{
label2.Text = "you can use this id";
}
}
Run Code Online (Sandbox Code Playgroud)
如果任何人知道为什么这不起作用或更好的方式它会挽救我的背面谢谢你.
更改private int id = 0;到public int Id { get; set; }.此外,所有的引用从更改id到Id.
将a添加using System.Linq到业务逻辑文件中.
更改if (Inv.Contains(id))到if (Inv.Any(x => x.Id == id))