我的应用程序是使用 Entity Framework 的 asp.net MVC3。我正在尝试使用以下方法从表中获取一列的列表作为数组:
public ActionResult Index()
{
//var list = db.CasesProgresses.ToList();
var SeriesList = GetSeriesList(Learner_ID);
return View();
}
public List<string> GetSeriesList(string item)
{
// get DataTable dt from somewhere.
List<string> sList = new List<string>();
foreach (CasesProgress row in db.CasesProgresses)
{
sList.Add(row[0].ToString());
}
return sList;
}
我得到两个错误:
Cannot apply indexing with [] to an expression of type 'caseprog.Models.CasesProgress' and
The name 'Learner_ID' does not exist in the current context
非常感谢您的建议。
最佳答案
您可能需要阅读 CasesProgress
对象的属性名称
改变这个
sList.Add(row[0].ToString());
到
sList.Add(row.YourPropertyNameHereWhichIsOfStringType);
如果属性不是字符串类型,(ex : Integer/decimal )您可以对其应用 ToString()
方法,然后添加到字符串集合中。
sList.Add(row.Learner_ID.ToString());
假设 Learner_ID
是 CasesProgress
类的数值类型的属性。
关于c# - MVC Entity Framework 将一列作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12519160/