我有一段代码在下面,它工作正常,但我想要别的东西得到结果。 此代码显示数据库表中与搜索条件匹配的第一行。在C#中使用Entity Framework搜索数据库
userName tbl = new userName();
bool flag = db.userName.Where(x => x.name == txtName.Text || x.City==txtCity.Text).Any();
if (flag)
{
tbl = db.userName.Where(x => x.name == txtName.Text || x.City==txtCity.Text).First();
userNameBindingSource.DataSource = tbl;
}
else
{
MessageBox.Show("This record does not exist in the database.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
但是如果我有多个行匹配搜索条件呢?我想在结果列表中显示它们全部,而不仅仅是符合条件的第一个。
我已经试过这一点,但出现了一些问题:
tbl = db.userName.Where(x => x.name == txtName.Text || x.City==txtCity.Text).All();
我应该如何改进上面的代码来实现这一目标?
小记:'tbl'是'userName',而不是'List'。 –
msitt
小记:您应该考虑不区分大小写的可能性 –
非常感谢您的快速回复。有用。这就是我想要得到的。 – Laki