/// <summary> /// 用户登录日志列表 /// </summary> /// <param name="userName">用户名</param> /// <param name="nowNumber">当前数字</param> /// <param name="pageSize">长度</param> /// <param name="rowCount">总行数</param> /// <returns>用户登录日志列表</returns> public IQueryable<object> LoadLogList(string userName, int nowNumber, int pageSize, out int rowCount) { IQueryable<object> result = null; if (String.IsNullOrEmpty(userName)) { result = from a in db.Set<AdminLoginLog>() join b in db.Set<Admin>() on a.AdminId equals b.AdminId into b_join from b in b_join.DefaultIfEmpty() orderby a.AdminLoginTime descending select new { AdminId = (Int32?)a.AdminId, AdminLoginAddress = a.AdminLoginAddress, a.AdminLoginBrowser, a.AdminLoginIP, a.AdminLoginLogId, a.AdminLoginTime, AdminUserName = b.AdminUserName }; } else { result = from a in db.Set<AdminLoginLog>() join b in db.Set<Admin>() on a.AdminId equals b.AdminId into b_join from b in b_join.DefaultIfEmpty() where b.AdminUserName == userName orderby a.AdminLoginTime descending select new { AdminId = (Int32?)a.AdminId, a.AdminLoginAddress, a.AdminLoginBrowser, a.AdminLoginIP, a.AdminLoginLogId, a.AdminLoginTime, AdminUserName = b.AdminUserName }; } rowCount = result.Count(); result = result.Skip(nowNumber).Take(pageSize); return result; }
这有没有什么好的方法动态拼接下where 语句,请各位指点下
你分开来 linq是延迟加载的,你先查询出result,然后判断String.IsNullOrEmpty(userName) 中执行result=result.where(m=>m.AdminUserName == userName)
而且你用orm了.返回object是什么鬼?
这样的话,m.AdminUserName这个好像是点不出来的吧
from a in db.Set<AdminLoginLog>() join b in db.Set<Admin>() on a.AdminId equals b.AdminId into b_join from b in b_join.DefaultIfEmpty() 那么这个我 select 什么啊
@yjq_叶: 你别把result定义成object就行了.用var去接..
ORM的一大目的就是强类型.你这样弄成object就白弄了.
不使用匿名类型或者把where在查询里写两遍
但是我查的时候是两个表的数据,怎么可以不用匿名类啊
你可以看一下where(参数)里面的参数是什么类型,当然可能动态构建,在这里传进去就可以了。我之前的一篇博问中有这样的代码,你可以参考 http://q.cnblogs.com/q/73479/
谢谢 谢谢 我好像会了
把需要where判断的Admin拆分出来,然后定义类似于下面的where谓词。调用Where方法。 最后再与其他的实体如AdminLoginLog去关联查数据
Expression<Func<BizDatOrders, bool>> where = p => p.OrderModifiedTime >= _TimeFrom && p.OrderModifiedTime <= _TimeTo;
额 我最后也是差不多这么解决的啊 非常感谢啊
你这段代码可以这样写:
result = from a in db.Set<AdminLoginLog>()
join b in db.Set<Admin>() on a.AdminId equals b.AdminId into b_join from b in b_join.DefaultIfEmpty()
where (String.IsNullOrEmpty(userName)?true:b.AdminUserName == userName)
orderby a.AdminLoginTime descending select new { AdminId = (Int32?)a.AdminId, AdminLoginAddress = a.AdminLoginAddress, a.AdminLoginBrowser, a.AdminLoginIP, a.AdminLoginLogId, a.AdminLoginTime, AdminUserName = b.AdminUserName };
谢谢谢谢 我有个疑问 加入 where (String.IsNullOrEmpty(userName)?true:b.AdminUserName == userName) 就是当userName为空的时候 数据库的查询字段会有where这个条件吗
@yjq_叶: 为空的话where条件永为true,这个可以有。用var接收延迟用where也可以。
http://esql.codeplex.com/
可以使用这个框架