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:
1st Try:
IList<string> myList = (from c in db.Table1
where .....
orderby c.CID
select c.MyColumn).ToList();
2nd Try:
var rows= from c in db.Table1
where .....
orderby c.CID
select c.MyColumn;
List<string> myList = rows.ToList();
3rd Try: // trying to put the result in an Array
var rows= from c in db.Table1
where .....
orderby c.CID
select c.MyColumn;
string[] myArray = new string[4]; // 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!
claudia888
Member
181 Points
435 Posts
Problem with putting a db selection result into a list?
Apr 29, 2012 11:09 PM|LINK
Hi,
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:
1st Try:
IList<string> myList = (from c in db.Table1
where .....
orderby c.CID
select c.MyColumn).ToList();
2nd Try:
var rows= from c in db.Table1
where .....
orderby c.CID
select c.MyColumn;
List<string> myList = rows.ToList();
3rd Try: // trying to put the result in an Array
var rows= from c in db.Table1
where .....
orderby c.CID
select c.MyColumn;
string[] myArray = new string[4]; // 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!
Thanks!
CLaudia
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Problem with putting a db selection result into a list?
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.
jsiahaan
Contributor
2304 Points
588 Posts
Re: Problem with putting a db selection result into a list?
Apr 29, 2012 11:50 PM|LINK
Hi,
Try this:
var rows = (from a in context.ProductCategories select a.Name).ToList(); List<string> myList = rows as List<string>;Hope can help
Indonesian Humanitarian Foundation
jsiahaan
Contributor
2304 Points
588 Posts
Re: Problem with putting a db selection result into a list?
Apr 30, 2012 12:02 AM|LINK
Hi,
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.
Have fun.
Indonesian Humanitarian Foundation
claudia888
Member
181 Points
435 Posts
Re: Problem with putting a db selection result into a list?
Apr 30, 2012 02:55 AM|LINK
HI Jannen,
Thanks for the suggestions. I tried your code - Unfortunately I still get the exactly same error message.
Thanks,
Claudia
claudia888
Member
181 Points
435 Posts
Re: Problem with putting a db selection result into a list?
Apr 30, 2012 03:02 AM|LINK
Hi Dan,
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.
Any thoughts?
Thanks,
Claudia
jsiahaan
Contributor
2304 Points
588 Posts
Re: Problem with putting a db selection result into a list?
Apr 30, 2012 03:32 AM|LINK
Hi Claudia,
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.
Have fun
Indonesian Humanitarian Foundation
jsiahaan
Contributor
2304 Points
588 Posts
Re: Problem with putting a db selection result into a list?
May 07, 2012 09:35 AM|LINK
Hi
Has the problem solved?
Regards
Indonesian Humanitarian Foundation
claudia888
Member
181 Points
435 Posts
Re: Problem with putting a db selection result into a list?
May 10, 2012 12:36 AM|LINK
Hi Jsiahaan,
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.
Thanks,
Claudia