MVC+EF实现多条件查询

img


MVC+EF实现多条件查询
MVC+EF实现多条件查询
MVC+EF实现多条件查询
MVC+EF实现多条件查询

就是lambda表达式

//构建实例
List<vyw_user> list = null;
IQueryable<vyw_user> result = Ctx.vyw_user;//Ctx表示EF上下文
//构建搜索条件
if (!string.IsNullOrEmpty(mobile) || !string.IsNullOrEmpty(usertype) || !string.IsNullOrEmpty(logintimestart) || !string.IsNullOrEmpty(logintimeend)) //搜索情况 假设有四个条件
            {
                #region 搜索情况
                if (mobile.Trim().Length > 0)//手机号码
                {
                    result = result.Where(c => c.mobile.Contains(mobile));
                }
                int usertypeid = Convert.ToInt32(usertype);
                if (usertypeid != -1)//用户类型
                {
                    result = result.Where(c => c.userType == usertypeid);
                }
                if (logintimestart.Trim().Length > 0 && logintimeend.Trim().Length > 0)//搜索查询时间
                {
                    DateTime bstart = Convert.ToDateTime(logintimestart);
                    DateTime bend = Convert.ToDateTime(logintimeend);
                    result = result.Where(c => c.loginTime >= bstart && c.loginTime <= bend);
                }
                #endregion
            }
//执行数据查询
count = result.Where(c => true).Count();//数据的统计
list = result.Where(c => true).ToList();//数据查询

以上内容是大体思路,具体的实现代码需要参考自身业务的数据表、结构、搜索字段等等。