Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 10, 2012 12:36 AM by claudia888
Apr 29, 2012 11:09 PM|LINK
I'm building a MVC3 app, and encountered a problem - I searched all over and saw other people having the same problem (with the same error message), but have not seen a good solution suggested.
What I'm trying to do is: put a db selection result into a list, or array. Here is my code:
IList<string> myList = (from c in db.Table1
var rows= from c in db.Table1
List<string> myList = rows.ToList();
3rd Try: // trying to put the result in an Array
string myArray = new string; // I know the length will be 4
myArray = rows.ToArray();
ALL the above 3 implementations gave me the same error message in VS:
NotSupportedException was unhandled by user code.
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.
Can anyone help or give a hint? Is there a workround? I've been stuck here for almost 2 days...really appreciate any advice!
Apr 29, 2012 11:12 PM|LINK
If your query returned it's results to a DataTable, you could always loop through the rows with a foreach construct.
Apr 29, 2012 11:50 PM|LINK
var rows = (from a in context.ProductCategories
List<string> myList = rows as List<string>;
Hope can help
Apr 30, 2012 12:02 AM|LINK
For better performance you may write like this:
List<string> myList = (from a in context.ProductCategories
select a.Name).ToList() as List<string>;
I have test them both with my prevoius reply.
Apr 30, 2012 02:55 AM|LINK
Thanks for the suggestions. I tried your code - Unfortunately I still get the exactly same error message.
Apr 30, 2012 03:02 AM|LINK
Thanks for trying to help. I actually tried that ... by using a froeach loop like this:
foreach (var item in rows)
But I got the exactly same error message for this line as well, for the "foreach" part - indicated by IntelliSense.
Apr 30, 2012 03:32 AM|LINK
It's so strange, I tested the code I have replied to you and It works fine.
Can you give the class of db.Table1?
There should be an error somewhere.
May 07, 2012 09:35 AM|LINK
Has the problem solved?
May 10, 2012 12:36 AM|LINK
I did not find out why I cannot put a db selection result directly into an array or list.
I did get around the problem by using:
ViewBag.MyList = rows.ToList();
Not sure why this works, but my original code does not. Either way, I continue to move forward now.