Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从数据库检索信息时只显示一个字段_C#_Asp.net_Sql Server_Visual Studio - Fatal编程技术网

C# 从数据库检索信息时只显示一个字段

C# 从数据库检索信息时只显示一个字段,c#,asp.net,sql-server,visual-studio,C#,Asp.net,Sql Server,Visual Studio,我从数据库检索信息时遇到问题。 我有一个函数 public List<string> Selectquery(string sql) { return GetContext().ExecuteQuery<string>(sql).ToList(); } 在另一个文件中,我调用函数,如下所示: List<string> SUsers = _DAO.Selectquery(sql); public class clients{ private strin

我从数据库检索信息时遇到问题。 我有一个函数

public List<string> Selectquery(string sql)
{
   return GetContext().ExecuteQuery<string>(sql).ToList();
}
在另一个文件中,我调用函数,如下所示:

List<string> SUsers = _DAO.Selectquery(sql);
public class clients{
private string username;
public string Username
{
   get
   {
      return username;
   }
   set
   {
      username = value;
   }
}
private string status_name;
public string Status_name
{
   get
   {
      return status_name;
   }
   set
   {
      status_name = value;
   }
}
private string firstName; 
public string FirstName 
{
   get
   {
      return firstName ;
   }
   set
   {
      firstName  = value;
   }
}
}
public class DynamicClass : DynamicObject
{
    private IDictionary<string, object> _values;

    public DynamicClass(IDictionary<string, object> values)
    {
        _values = values;
    }
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (_values.ContainsKey(binder.Name))
        {
            result = _values[binder.Name];
            return true;
        }
        result = null;
        return false;
    }
}
var values = new Dictionary<string, object>();
values.Add("Username", "value which you read from database");
values.Add("Status_name", "value which you read from database");
values.Add("FirstName","value which you read from database" );

var client = new DynamicClass(values);
我只看到用户名,其他字段消失了。我有
IEnumerable
的更改列表,结果是一样的。 所以我不知道发生了什么。 谢谢你的帮助。
谢谢。

创建如下类:

List<string> SUsers = _DAO.Selectquery(sql);
public class clients{
private string username;
public string Username
{
   get
   {
      return username;
   }
   set
   {
      username = value;
   }
}
private string status_name;
public string Status_name
{
   get
   {
      return status_name;
   }
   set
   {
      status_name = value;
   }
}
private string firstName; 
public string FirstName 
{
   get
   {
      return firstName ;
   }
   set
   {
      firstName  = value;
   }
}
}
public class DynamicClass : DynamicObject
{
    private IDictionary<string, object> _values;

    public DynamicClass(IDictionary<string, object> values)
    {
        _values = values;
    }
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (_values.ContainsKey(binder.Name))
        {
            result = _values[binder.Name];
            return true;
        }
        result = null;
        return false;
    }
}
var values = new Dictionary<string, object>();
values.Add("Username", "value which you read from database");
values.Add("Status_name", "value which you read from database");
values.Add("FirstName","value which you read from database" );

var client = new DynamicClass(values);
并使用
List
为您的方法映射结果

编辑:如果您的查询是动态的,您可以使用C#的
dynamic
规范:

创建如下所示的动态实体:

List<string> SUsers = _DAO.Selectquery(sql);
public class clients{
private string username;
public string Username
{
   get
   {
      return username;
   }
   set
   {
      username = value;
   }
}
private string status_name;
public string Status_name
{
   get
   {
      return status_name;
   }
   set
   {
      status_name = value;
   }
}
private string firstName; 
public string FirstName 
{
   get
   {
      return firstName ;
   }
   set
   {
      firstName  = value;
   }
}
}
public class DynamicClass : DynamicObject
{
    private IDictionary<string, object> _values;

    public DynamicClass(IDictionary<string, object> values)
    {
        _values = values;
    }
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (_values.ContainsKey(binder.Name))
        {
            result = _values[binder.Name];
            return true;
        }
        result = null;
        return false;
    }
}
var values = new Dictionary<string, object>();
values.Add("Username", "value which you read from database");
values.Add("Status_name", "value which you read from database");
values.Add("FirstName","value which you read from database" );

var client = new DynamicClass(values);

我认为这与试图将数据集结果拟合到数据集内部有关。string意味着它将只返回一个字符串,这可能就是您获得单个用户名的原因请看这里| |我可以这样做,但问题是我要选择的字段是动态创建的。我正在做一个动态查询,所以我不知道在创建类时是否创建了字段的所有可能值,这将解决问题。@user3224414我编辑了答案,希望它有用。