我正在使用 Linq 进行查询,并且希望能够使用反射在“选择”部分中获取我想要返回的属性列表。我已尝试以下方法但无济于事:
string[] paramList = new[]{"AppId","Name"};
var query =
from entity in
_ctx.App
select new {entity.GetType().GetProperties().Where(prop=>paramList.Contains(prop.Name) )};
我在这里缺少什么?
在 EF 查询中使用反射时,您需要自己编写表达式。查看这些现有问题以获取更多信息
- 帮助 Linq 和泛型。在查询中使用 GetValue https://stackoverflow.com/questions/4546463/help-with-linq-and-generics-using-getvalue-inside-a-query
- 如何反映 T 来构建查询的表达式树? https://stackoverflow.com/questions/4632778/how-to-reflect-over-t-to-build-an-expression-tree-for-a-query
- 使用 GetValue 的 IQueryable 动态排序/过滤失败 https://stackoverflow.com/questions/4611559/iqueryable-dynamic-ordering-filtering-with-getvalue-fails
问题不在于 Linq 本身,而是因为您的查询被解析为实体框架无法理解的表达式树。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)