下面是存储库,其中定义了empmodel类型iEnumerable并执行了一些数据库命令,该命令成功返回数据,现在想要使用变量在控制器上获取此记录但无法获取。
public IEnumerable<empModel> GetEditData(int id, int deployment_id)
{
NpgsqlConnection conn = new NpgsqlConnection();
conn.ConnectionString = conn_str;
using (conn)
{
// Open connection
if (conn.State == ConnectionState.Closed)
conn.Open();
using (NpgsqlCommand command = common_repo.GetSqlCommandQuery("select * from get_data(" + id + ", " + deployment_id + ", 'data'); fetch all in \"data\";", conn))
{
using (NpgsqlDataReader dr = command.ExecuteReader())
{
while (dr.Read()) ;
// Go to next result set
dr.NextResult();
// Iterate through result set to get result set
while (dr.Read())
{
yield return empModel.SetValues(dr);
}
} // reader closed and disposed up here
} // command disposed here
} //connection closed and disposed here
}
public ActionResult Edit(int id)
{
//var lead_data = db.Leads.Find(lead_id);
var lead_data = uow.LeadRepo.GetEditData(id,1);
}
这显示没有数据。
答案 0 :(得分:0)
IEnumerable和yield return在需要时返回数据(它的懒惰)。 如果要查看数据必须加载它(例如使用.First()或.ToList()) 你可以这样做:
public ActionResult Edit(int id)
{
var lead_data = uow.LeadRepo.GetEditData(id,1).ToList();
}