MVC中,使用linq,如何解决数据权限问题?
以前直接用sql语句执行的,例如
select * from User where 1=1
select * from User where 1=1 and DeptCode like '0041%' and PositionLevel > 3
#region 查询处理 var query_products = EF.V_OrderDetail.Where(a => a.ActionType == 0 && a.Company_ID == UserInfo.CompanyID); if (orderstate.HasValue) { query_products = query_products.Where(b => b.State==orderstate); } if (product.HasValue && product.Value > 0) { query_products = query_products.Where(b => b.Product_ID == product.Value); } if (datebegin != null && datebegin.IsDatetime()) { DateTime begin = datebegin.ToDateTimeShort(); query_products = query_products.Where(a => a.ActionTime >= begin); } if (dateend != null && dateend.IsDatetime()) { DateTime end = dateend.ToDateTimeShort().AddDays(1); query_products = query_products.Where(a => a.ActionTime < end); } if (!string.IsNullOrEmpty(disti_name)) { query_products = query_products.Where(a => a.CustomerCompany.Contains(disti_name)); } if (!string.IsNullOrEmpty(orderSN)) { query_products = query_products.Where(a => a.OrderSN.Contains(orderSN)); } #endregion
[解决办法]
1L正解,如果要想进一步灵活控制查询条件生成,可以Google“Linq 表达式树”。