I’m trying to display a list coming from my database using Linqtosql but I guess I’m making it wrong.
I wrote the SPROC to retrieve the data here if you want further info:
And this is my code:
protected List<MySubject> GetSubjects(int userID, string Datetime)
{
userID = user_id;
string[] dates = Datetime.Split('-');
int year = int.Parse(dates[2]);
int month = int.Parse(dates[1]);
int day = int.Parse(dates[0]);
ClassDAO classClass = new ClassDAO();
List<MySubject> myTodaySubjects = GetSubjectsLinq(userID, day);
GetSubjectsLinq(userID, day);
return myTodaySubjects;
}
private List<MySubject> GetSubjectsLinq(int UserId, int day)
{
MySchoolDBDataContext db = new MySchoolDBDataContext();
string Suserid = UserId.ToString();
string Sday = day.ToString();
var sub = db.GetSubjects(Sday , Suserid);
return sub.ToList<MySubject>();
}
This Line "return sub.ToList<MySubject>();" shows this error :
Error 21 Instance argument: cannot convert from
'System.Data.Linq.ISingleResult' to 'System.Collections.Generic.IEnumerable'.
Do you know how can I fix it ?
Check your
method(stored procedure). It looks like it is returning only one single value of a certain type, which means that it cannot be assigned to an
IEnumerable<T>such as aList<T>.Since you use a stored procedure in LINQ, it’ll generate a class T(procedure name + “Result”) for you automatically to store the data. The returned type is always
ISingleResult<T>.So, you can try the following:
or just use
db.GetSubjects (Sday , Suserid).ReturnValue;