日期:2014-05-17  浏览次数:20430 次

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;