我如何引用基于动态字段名Linq中的一个字段 [英] How do I reference a field in Linq based on a dynamic fieldname

查看:116
本文介绍了我如何引用基于动态字段名Linq中的一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对于不良的问题称号的歉意 - 不能完全肯定,如果我问正确的事情。

Firstly, apologies for the bad question title - not entirely sure if I am asking the correct thing.

通常我可以做以下访问一个字段:

Normally I can do the following to access a field:

MyTables table = dc.MyTables.SingleOrDefault(p => p.id == someId);
somevalue = table.samplefield;

在此实例变量的 someValue中的最终会具有的价值现场的 samplefield

In this instance the variable somevalue would end up having the value of the field samplefield.

现在我有我想要的填充变量的情况下,但我不知道表字段的名称在设计时。然而,我这样做,有这个字段名的字符串。是不是因此可以使用此字符串来获取一个值?

Now I have a scenario where I want to populate a variable, but I don't know the name of the table field at design time. I do however, have this fieldname in a string. Is it therefore possible to fetch a value using this string?

希望这是有道理的!

推荐答案

您需要使用反射,这样的:(未经测试)

You need to use reflection, like this: (Untested)

somevalue = typeof(MyTable).GetProperty(fieldName).GetValue(table, null);

这篇关于我如何引用基于动态字段名Linq中的一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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