LINQ stored procedure return value to a string array?
I have a stored procedure in DBML and I try to pass the result to "sqlQryArray" as an array(1 dimensional array). But the bottom code causes error like below message. What else should be done?
Error 1 Value of type '1-dimensional array of aaaDatabase.stp_GetSomethingResult' cannot be converted to '1-dimensional array of String' because 'aaaDatabase.stp_GetSomethingResult' is not derived from 'String'.
The return result from stored procedure is a just list of first names of students(only one column)
Dim sqlQry = aaaLINQ.stp_GetSomething(bbb,ccc,ddd)
Dim sqlQryArray As开发者_如何学Go String() = sqlQry.ToArray()
Some background
Once the stored procedure result has been returned, it is converted into a ISingleResult, which implements IEnumerable, and returned to the calling client. As ISingleResult is a generic class, an object with the same name as the sproc is created with the same properties as returned by the stored procedure.
When using stored procedures they can only return items of type ISingleResult or IMultipleResult. This causes a number of problems when using the created objects. Firstly, in your client code you will have to manually convert the result to something like ToList() if you want to bind the objects to a data source. If you convert the objects to a list then they will not be change-tracked. Finally, you cannot benefit from deferred loading when using stored procedures as the call to the database is executed when the method is called.
Read the complete article here: http://blog.benhall.me.uk/2008/05/linq-to-sql-stored-procedure-vs.html
Check this too: a possible solution where you immediately re-map the result to your own POCO representation. You can also use a LINQ query to manipulate your result:
LINQ to SQL: Stored Procedure Results
The result from your stored procedure is probably a record that resembles something like this (POCO C# notation, I hope you can read it):
class Student {
public string Name { get; set; }
}
So the statement:
sqlQry.ToArray()
returns an array of Student objects. If all you need is the name, select the name from the record:
sqlQuery.Select(student => student.Name).ToArray()
this will return an array of string (containing the student names).
(Sorry all examples in C#, my VB skills to bad to type code without intellisense).
精彩评论