动态对象 - 整洁的属性名称?
我正在使用一些代码来执行 SQL 并返回动态对象的 IEnumerable。代码是 此处。
我有一个表,其列名称例如 APPLICATION_NAME;
在我的对象中,我必须像这样引用它:
var results = ReturnResults("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=rawdb;Data Source=mypc", "SELECT * FROM APPLICATIONS");
string name = results.First().APPLICATION_NAME;
有没有办法使属性名称解析为更整洁的内容? IE。
string name = results.First().ApplicationName;
谢谢
I'm using some code to execute a SQL and return a IEnumerable of dynamic objects. The code is here if you want to see it.
I have a table with a column name such as APPLICATION_NAME;
In my object I have to reference it like so:
var results = ReturnResults("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=rawdb;Data Source=mypc", "SELECT * FROM APPLICATIONS");
string name = results.First().APPLICATION_NAME;
Is there a way to make property names resolve to something tidier? ie.
string name = results.First().ApplicationName;
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些
ToCamelCase()
扩展(只是 谷歌搜索)。但是如果你将它实现到你的动态对象中。你怎么知道该用哪个闪亮的名字来代替一个丑陋的列名呢?相反,您应该重写 select 语句以返回漂亮的名称,而不是在动态对象中实现某些算法。这条语句怎么样:
这样每个读过sql语句的人都清楚如何通过动态对象访问列。
There are some
ToCamelCase()
extensions out there (just google it). But if you would implement it into your dynamic object. How do you know which shiny name you have to take for an ugly column name?Instead you should rewrite your select statement to return nice names, instead of implementing some algorithm into the dynamic object. How about this statement:
So it is clear to everyone who reads the sql statement how to access the columns through the dynamic object.