实体框架提供列名作为字符串变量 [英] Entity Framework providing column names as string variable

查看:64
本文介绍了实体框架提供列名作为字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找获取类似内容的方法:

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.

推荐答案

我不确定EF是否为您提供了一种基于字符串获取属性值的方法属性的名称,但可以使用反射。

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.

string name = typeof(Customer)
    .GetProperty("first name")
    .GetValue(query.First(), null) as string;

我猜您正在处理的EF类称为 Customer

I'm guessing the EF class you're dealing with is called Customer.

这篇关于实体框架提供列名作为字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆