[英]MVC Entity Framework get one column as an array
my application is asp.net MVC3 using Entity Framework. 我的应用程序是使用实体框架的asp.net MVC3。 I am trying to get a list of one column from a table as an array using the following:
我正在尝试使用以下方法从表中获取一列的列表作为数组:
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;
}
I get two errors: 我得到两个错误:
Cannot apply indexing with [] to an expression of type 'caseprog.Models.CasesProgress' and
The name 'Learner_ID' does not exist in the current context
Would appreciate your suggestions. 感谢您的建议。
You probably need to read the Property name of your CasesProgress
object 您可能需要读取
CasesProgress
对象的属性名称
Change this 改变这个
sList.Add(row[0].ToString());
to 至
sList.Add(row.YourPropertyNameHereWhichIsOfStringType);
If the Property is not of string type, ( ex : Integer / decimal ) you may apply ToString()
method on that and then add to the string collection. 如果该属性不是字符串类型( 例如:Integer / decimal ),则可以对此应用
ToString()
方法,然后将其添加到字符串集合中。
sList.Add(row.Learner_ID.ToString());
Assuming Learner_ID
is a property of CasesProgress
class with numeric type. 假设
Learner_ID
是具有数字类型的CasesProgress
类的属性。
foreach (CasesProgress row in db.CasesProgresses)
{
sList.Add(row.SomePropertyYouNeed.ToString());
}
if you are using a foreach, there is no [0]
. 如果您使用的是foreach,则没有
[0]
。 If you were using a normal for (int i =0; i < list.Count; i ++;)
then you would be able to do that. 如果您
for (int i =0; i < list.Count; i ++;)
使用法线for (int i =0; i < list.Count; i ++;)
则可以做到这一点。 But then ofcourse you would have to change the 0
to i
, and you would still need to access a property. 但是,当然,您必须将
0
更改为i
,并且仍然需要访问属性。 Well you wouldn't have to do that if your List had a different type, but in your current example you will need to add a String. 那么你就不必这样做,如果你的列表有不同的类型,但在当前的例子中,你将需要添加一个字符串。
The name 'Learner_ID' does not exist in the current context
GetSeriesList(Learner_ID);
Learner_ID - is not defined in your action and you can remove it, because parameter in GetSeriesList(string item) is not used (also remove parameter). Learner_ID-未在您的操作中定义,您可以删除它,因为未使用GetSeriesList(字符串项目)中的参数(也删除了参数)。
Cannot apply indexing with [] to an expression of type 'caseprog.Models.CasesProgress'
In foreach
row is separate item from collection, that's why you shoud access it by property with dot notation row.Property
. 在
foreach
行中,集合中有单独的项目,这就是为什么您应该通过属性使用点符号row.Property
访问它。
Why not just keep it simple and use the 'usual' syntax 为什么不只是保持简单并使用“常规”语法
var list = db.CasesProgresses.Select(o=>o.YourPropertyName).ToList();
If its an int and you need it as a string list simply replace YourPropertyName with YourPropertyName.ToString() above 如果它是一个int,并且您需要它作为字符串列表,则只需用上方的YourPropertyName.ToString()替换YourPropertyName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.