使用字符串变量作为别名 [英] use a string variable as alias

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

问题描述


我有一个字符串变量myFieldname.
如何在linq表达式中将其值用作别名?

Hi,
I have a string variable, myFieldname.
How can I use its value as a alias in a linq expression?

string myFieldname = "theName";
var q = (from d in data select new { d.Id, myFieldname = d.Amount });



我希望"theName"成为别名,而不是"myFieldname"本身.



I want ''theName'' be the alias not the ''myFieldname'' itself.

推荐答案

不,您不能那样做.要将变量插入到字符串中然后执行它,需要使用反射.也就是说,将根据"myfieldname"中的值而不是myFieldname中命名的字段检查d.Amount.
No, you can''t do that. To insert a variable in to a string and then execute it, requires the use of reflection. that is to say, d.Amount will be checked against the value in ''myfieldname'', not the field named in myFieldname.


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

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