实体框架提供列名称作为字符串变量
我正在寻找获得类似以下内容的方法:
string _col1 = "first name";
string _name;
var query = from c in ctx.Customers select c;
_name = query.FirstOrDefault().[_name];
据我所知,我只能获得强类型字段名称,但我想将它们作为字符串变量提供。
I'm looking for way to get something like this:
string _col1 = "first name";
string _name;
var query = from c in ctx.Customers select c;
_name = query.FirstOrDefault().[_name];
As far as I can see I can only get strongly typed field names but I would like to provide them as string variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定 EF 是否为您提供了一种根据属性的字符串名称获取属性值的方法,但您可以使用反射。
我猜您正在处理的 EF 类称为
Customer
。I'm not sure if EF provides you with a way to get a property value based on the string name of the property, but you can use reflection.
I'm guessing the EF class you're dealing with is called
Customer
.为此,您需要使用反射。如果您尝试按动态选择的列进行过滤,您可以尝试这样的操作:
我您想要做的是选择特定列,然后您可以使用相同的原理来生成兰巴表达式并在选择中使用它(减去条件)
You need to use reflection for this. If you are trying to filter by a dynamicly selected column, you can try something like this:
I what you are trying to do is selecting a particular column, then you can use the same principle for generating the lamba expression and using it in the select (minus the condition)
由于某种原因,它对我不起作用。
这是我类似的工作解决方案:
}
For some kind of reason it won't work for me.
That's my similar working solution:
}