我有两张桌子
table01
NameID,名称(PK = NameID )
1,John 2,Rose
10,X先生
11,X夫人
table02
CarID NameID CarName (PK = CarID,FK = NameID )关系
1,1,奔驰
2,1,宝马
3,yYy
4,10,xXx
5,11,zZz
在我的表格中,我有两个组合框
combobox1数据= table01数据
private void Form1_Load(object sender, EventArgs e)
{
using (UnitOfWork db = new UnitOfWork())
{
//لود کردن کمبوباکس لایه
comboBox1.DataSource = db.table01Repository.Get();
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "NameID";
}
}combobox2数据= table02数据如果NameID = NameID
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue.ToString() != null)
{
using (UnitOfWork db = new UnitOfWork())
comboBox2.DataSource = db.table02Repository.GetNameIDByFilter(comboBox1.SelectedValue.ToString());
comboBox2.DisplayMember = "CarName";
comboBox2.ValueMember = "CarID";
}
}现在我的问题是如果我把combox1改为1(约翰)
我的数据incombobox2 =
本兹
宝马
xXx
zZz
这是因为在SelectedIndexChanged中(在table02中计算所有1)
如何将代码更改为1=1(仅1),而不是1= 1+ 0或1=1+1+0
我的存储库
public IEnumerable<table02> GetNameIDByFilter(string parameter)
{
return db.table02.Where(g => g.NameID.ToString().Contains(parameter)).ToList();
}发布于 2020-04-25 12:20:03
我发现我错了
IRepository
IEnumerable<table02> GetNameIDByFilter(string parameter);存储库
public IEnumerable<table02> GetNameIDByFilter(string parameter)
{
if (!int.TryParse(parameter, out int value))
{
return new List<table02>();
}
return db.table02.Where(g => g.NameID == value).ToList();
}形式代码
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = comboBox1.SelectedValue.ToString();
using (UnitOfWork db = new UnitOfWork())
if (!string.IsNullOrEmpty(selectedValue))
{
comboBox2.DataSource = db.table02Repository.GetNameIDByFilter(selectedValue);
comboBox2.DisplayMember = "CarName";
comboBox2.ValueMember = "CarID";
}https://stackoverflow.com/questions/61407074
复制相似问题