在数据库中搜索仅在带有链接按钮的 Gridivew 中显示名字和姓氏。
在第一张图片(1)中,我只搜索名字,并显示名字和姓氏。在第二个图像(2)中,我只搜索第一个字符并显示它,但我显示它 5 次,为什么?只想要一次?
这是我的代码
private void GetEmployee()
{
var db = new knowitCVdbEntities();
var employee = (from p in db.EMPLOYEES
where
p.firstname.Contains(TextBoxSearch.Text) ||
p.lastname.Contains(TextBoxSearch.Text) ||
p.position.Contains(TextBoxSearch.Text)
select p).ToList();
foreach (var vEmp in employee)
{
if (vEmp != null)
{
HiddenFieldID.Value = vEmp.employee_id.ToString();
if (Session["DataTableSearch"] != null)
{
_dt = (DataTable)Session["DataTableSearch"];
}
else
{
_dt.Columns.Add("Firstname");
_dt.Columns.Add("employeeId");
}
//_dt.Rows.Clear();
DataRow dr = _dt.NewRow();
dr["Firstname"] = vEmp.firstname+" "+vEmp.lastname;
dr["employeeId"] = vEmp.employee_id;
_dt.Rows.Add(dr);
Session["DataTableSearch"] = _dt;
GridViewDisplayName.DataSource = _dt;
GridViewDisplayName.DataBind();
}
}
}