在Controller中增加一个Action,查询DBContext.Provinces,并用上面扩展的SelectPermissionDynamic方法映射到动态类型返回当前用户权限范围内可见的内容。代码片段如下:
[HttpGet, Route(nameof(Visibility))] public IActionResult Visibility(string id) { var querable = _dbContext.Provinces.SelectPermissionDynamic(id.Split(',')).Take(2); return Json(querable.ToList()); }测试case:
访问/Test/Visibility?id=2,3,预期返回Code和Name属性;
访问/Test/Visibility?id=5,6,预期返回Code属性;
如下图所示,返回符合预期,测试通过!
参考文档:https://stackoverflow.com/questions/606104/how-to-create-linq-expression-tree-to-select-an-anonymous-type