【问题标题】:How do I get the name of the variable? [duplicate]如何获取变量的名称? [复制]
【发布时间】:2017-04-26 22:21:53
【问题描述】:

我想使用 sqlite 数据库返回一些值,代码如下:

    public Dictionary<string, string> GetRegistry()
    {
        var table = this.connection.Table<Registry>();
        Dictionary<string, string> RegistryItems = new Dictionary<string, string>();

        foreach (var item in table)
        {

        }
    }

鉴于var item 代表一个sql 表项,所以说item.Age 将是一个年龄等等。我希望能够返回一个字典,我可以在其中执行以下操作:

Dictionary registry = this.GetRegistry();
int myAge = registry["Age"];

我应该在foreach 子句中做些什么来实现这一点? 提前致谢!

【问题讨论】:

  • 如果您使用的是 C# 6,我认为您可以通过 nameof(item.Age) 恢复“年龄”。但是有些东西告诉我列名应该已经在你的对象中了......
  • 你想遍历表格的每一列吗?
  • 是的,我不能做 nameof(item.Age) 因为它是一个循环,列的名称是动态的
  • 在php中我可以做到foreach ($array as $key =&gt; $value) {}$key代表列的名称

标签: c# android xamarin


【解决方案1】:

鉴于该表表示 sql 表,您可以执行以下操作:

public Dictionary<string, string> GetRegistry()
        {


            var table = this.connection.Table<Registry>();
            Dictionary<string, string> RegistryItems = new Dictionary<string, string>();

            var ages = table.Select(X => x.Name == "Age").ToList();

        }

【讨论】:

    猜你喜欢
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多