Entity Framework 多条件查询语句不会写
////示範:多條件查詢
using (TestEntities te = new TestEntities())
{
////寫法一
StringBuilder sb = new StringBuilder();
if (!string.IsNullOrEmpty(TextBoxId.Text))
{
sb.Append(" and it.user_id = " + TextBoxId.Text);
}
if (!string.IsNullOrEmpty(TextBoxName.Text))
{
sb.Append(" and it.user_name = '" + TextBoxName.Text + "'");
}
if (!string.IsNullOrEmpty(TextBoxAddress.Text))
{
sb.Append(" and it.user_address = '" + TextBoxAddress.Text + "'");
}
List<user> users;
////取得資料
if (sb.Length != 0)
{
users = te.user.Where(sb.ToString().Substring(4)).ToList();
}
else
{
users = te.user.Select(a => a).ToList();
}
GridView1.DataSource = users;
GridView1.DataBind();
}
sb中有时间的比较大于或小于某个时间断,怎样查询某天的全部记录。
[解决办法]
http://www.cnblogs.com/ahui/archive/2011/08/04/2127282.html
[解决办法]
users =from u in te.user select u;
if (!string.IsNullOrEmpty(TextBoxId.Text))
{
users= users.Where(u=>u.user_id ==Convert.ToInt32(TextBoxId.Text));
}
if (!string.IsNullOrEmpty(TextBoxName.Text))
{
users= users.Where(u=>u.user_name ==TextBoxName.Text );
}
if (!string.IsNullOrEmpty(TextBoxAddress.Text))
{
users= users.Where(u=>u.user_address ==TextBoxAddress.Text );
}
时间比较用:System.Data.Linq.SqlClient.SqlMethods.DateDiffDay()