62,046
社区成员
发帖
与我相关
我的任务
分享
//分页
public static List<T> GetPageList<T, TKey>(Func<T, bool> where, Func<T, TKey> order, int pageIndex, int pageSize, out int Total)
where T : class
{
MyCrmContext db = new MyCrmContext();
Total = db.Set<T>().Where(where).Count();
var list = db.Set<T>().Where(where).OrderBy<T, TKey>(order).Skip((pageIndex - 1) * pageSize).Take(pageSize);
return list.ToList();
}
//调用
this.rptList.DataSource = GetPageList<Model.sys_user, object>(u=u.id>0, u => u.add_time, page, pageSize, out totalCount);
this.rptList.DataBind();
protected string CombSqlTxt(string _keywords)
{
StringBuilder strTemp = new StringBuilder();
_keywords = _keywords.Replace("'", "");
if (!string.IsNullOrEmpty(_keywords))
{
strTemp.Append(" and (user_name like '%" + _keywords + "%' ");
}
return strTemp.ToString();
}
Expression<Func<Model.sys_user, bool>> func = u => true;
func = u => u.role_type >= 1;//这个条件会被下面的覆盖掉,即生成的SQL没有role_type的判断,应该怎么写才会两个条件同时起作用
if (!string.IsNullOrEmpty(this.keywords))
{
func = i => i.user_name.Contains(this.keywords) ;
}
Expression<Func<Model.sys_user, bool>> func = u => u.role_type >= 1;
if (!string.IsNullOrEmpty(this.keywords))
{
func = i => i.user_name.Contains(this.keywords) ;
}
this.rptList.DataSource = EntityHelper.GetPageList<Model.sys_user, object>(func, u => u.add_time, page, pageSize, out totalCount);
this.rptList.DataBind();
报错:Unable to cast the type 'System.DateTime' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.
Func<sys_user, bool> filter=m=>m.user_name=="keyword";
Func<Model.sys_user, bool> func = (i) => true;
if (!string.IsNullOrEmpty(this.keywords))
{
func = i => func(i) && i.user_name.Contains(this.keywords) ;
}
这样写吗?func(i) 代表什么Func<int, bool> func = (i) => true;
if (true)
{
func = i => func(i) && true;
}
给你参考例子