简体   繁体   English

使用 LinQ 到 SQL 搜索栏

[英]Search bar by using LinQ to SQL

Please could someone help me with this problem?请问有人可以帮我解决这个问题吗? I am trying to do a search form.我正在尝试做一个搜索表单。 everything works fine, but I type a username it shows me the result of the username that I typed, however, when I enter an ID or name it shows me the whole table一切正常,但是我输入了一个用户名,它会显示我输入的用户名的结果,但是,当我输入一个 ID 或名称时,它会显示整个表

 protected void btnsearch_Click(object sender, EventArgs e) { var st = from i in db.Login1s where i.ID.Contains(TxtID.Text) select new {ID = i.ID, Name = i.Name, Username = i.Username, Password = i.Password}; GridView1.DataSource = st.ToList(); GridView1.DataBind(); var na = from n in db.Login1s where n.Name.Contains(TxtName1.Text) select new { ID = n.ID, Name = n.Name, Username = n.Username, Password = n.Password }; GridView1.DataSource = na.ToList(); GridView1.DataBind(); var us = from u in db.Login1s where u.Username.Contains(TxtUsername.Text) select new { ID = u.ID, Name = u.Name, Username = u.Username, Password = u.Password }; GridView1.DataSource = us.ToList(); GridView1.DataBind(); } } }

You are effectively binding only the last datasource to your view.您实际上只将最后一个数据源绑定到您的视图。 Combine your Linq statements in one by using && in your where condition.通过在where条件中使用&&将您的 Linq 语句合并为一个。

where i.ID.Contains(TxtID.Text) && i.Name.Contains(TxtName1.Text) …

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM