首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Entity Framework 多条件查询话语不会写

2013-07-16 
Entity Framework 多条件查询语句不会写////示範:多條件查詢using (TestEntities te new TestEntities()

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()


[解决办法]
首先写一个基本的查询,如
var datas = from o in db.Orders select o;
然后:
datas = datss.Where(o => o.xx == 'xx');
datas = datas.Where(c => c.xx.Contains('xx');

但是这种方式吧,只适合拼接 And 的查询,对 Or 的查询拼接是没用的。Or 的查询必须在一个 Where 中完成。
[解决办法]
额,又乱来了

把字符串当委托实现用,除非你自己提供linqproivide才可以

xxx.where(p=>条件1)
   .where(p=>条件2)

如果你自己没办法写表达式树解析,也没能力提供linqproivide

那么请分开逐次过滤,反正EF本身是延迟策略,并不会有多少效率问题
[解决办法]
http://blog.csdn.net/q107770540/article/details/5724013

热点排行