使用反射获取linq查询中的select字段 [英] Using reflection to obtain select fields in a linq query
本文介绍了使用反射获取linq查询中的select字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Linq进行查询,并希望能够使用反射来获取我在select部分中返回的属性列表。我尝试以下操作无效:
I am using Linq for my queries and would like to be able to get a list of properties I want returned in the "select" portion using reflection. I've tried the following to no avail:
string[] paramList = new[]{"AppId","Name"};
var query =
from entity in
_ctx.App
select new {entity.GetType().GetProperties().Where(prop=>paramList.Contains(prop.Name) )};
我在这里缺少什么?
推荐答案
在EF查询中使用反射时,您需要自己编写表达式。查看这些现有问题了解更多信息
When working with reflection inside a EF query you would need to write the expression yourself. Look at these existing question for more information
- Help with Linq and Generics. Using GetValue inside a Query
- How to reflect over T to build an expression tree for a query?
- IQueryable<> dynamic ordering/filtering with GetValue fails
问题不是Linq本身,而是因为您的查询被解析为表达式树实体框架不明白。
The problem is not Linq itself, but because your query is parsed into an Expression Tree which Entity Framework doesn't understand.
这篇关于使用反射获取linq查询中的select字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文