Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 29, 2012 01:32 AM by IgorKitsula
Member
193 Points
481 Posts
Apr 28, 2012 09:41 PM|LINK
HI,
I'm building a MVC3 app. Can I use "ToArray()" to make a db selection result into an array? Here is an example:
for (int i = 0; i < 5; i++) { var rows = from c in db.Table1 where ... select c.ColumnValue; string[] newArray = new string[4];
newArray = rows.ToArray();
}
Question: Is this correct? If not, how do I acheive what I want? I want to put the values in rows into an array.
Or, Option2:
int i = 0; foreach (var item in XYZ) { newArray[i] = item; i++; }
Is this correct?
Note: for both implementations above, I can compile the app, but when I run it, I get this error:
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.
Thanks!!
Claudia
144 Points
33 Posts
Apr 28, 2012 09:52 PM|LINK
First, you don't need loop.
Second - I would suggest to use List (List<string> in your case).
var rows = from c in db.Table1 where ... select c.ColumnValue;
List<string> result = rows.ToList();
or
var = rows.ToList();
or return list to the view:
return View(rows.ToList())
Contributor
3970 Points
1096 Posts
Apr 28, 2012 10:15 PM|LINK
Why do you need an array in the first place? What do you want to do that you can't do with a datatable?
Apr 29, 2012 12:02 AM|LINK
Hi IgorKitsula,
Thanks for the input. I tried your suggestion "List<string> result = rows.ToList();" and "var = rows.ToList()", but got the same result as I had.
When I run the app (F5), this line gave the same error message:
A question: in rows (after a db selection like this) - how is the information stored? in what format?
Thanks,
Apr 29, 2012 01:32 AM|LINK
Can you provide complete code of your controller and view?
claudia888
Member
193 Points
481 Posts
How to put a db selection result into an array?
Apr 28, 2012 09:41 PM|LINK
HI,
I'm building a MVC3 app. Can I use "ToArray()" to make a db selection result into an array? Here is an example:
for (int i = 0; i < 5; i++)
{
var rows = from c in db.Table1
where ...
select c.ColumnValue;
string[] newArray = new string[4];
newArray = rows.ToArray();
}
Question: Is this correct? If not, how do I acheive what I want? I want to put the values in rows into an array.
Or, Option2:
int i = 0;
foreach (var item in XYZ)
{
newArray[i] = item;
i++;
}
Is this correct?
Note: for both implementations above, I can compile the app, but when I run it, I get this error:
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.
Thanks!!
Claudia
IgorKitsula
Member
144 Points
33 Posts
Re: How to put a db selection result into an array?
Apr 28, 2012 09:52 PM|LINK
First, you don't need loop.
Second - I would suggest to use List (List<string> in your case).
var rows = from c in db.Table1
where ...
select c.ColumnValue;
List<string> result = rows.ToList();
or
var = rows.ToList();
or return list to the view:
return View(rows.ToList())
- Igor
Weblog: http://kitsula.com
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: How to put a db selection result into an array?
Apr 28, 2012 10:15 PM|LINK
Why do you need an array in the first place? What do you want to do that you can't do with a datatable?
claudia888
Member
193 Points
481 Posts
Re: How to put a db selection result into an array?
Apr 29, 2012 12:02 AM|LINK
Hi IgorKitsula,
Thanks for the input. I tried your suggestion "List<string> result = rows.ToList();" and "var = rows.ToList()", but got the same result as I had.
When I run the app (F5), this line gave the same error message:
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.
A question: in rows (after a db selection like this) - how is the information stored? in what format?
Thanks,
Claudia
IgorKitsula
Member
144 Points
33 Posts
Re: How to put a db selection result into an array?
Apr 29, 2012 01:32 AM|LINK
Can you provide complete code of your controller and view?
- Igor
Weblog: http://kitsula.com