I need to convert linq query result to list. I tried the following code:
var qry = from a in obj.tbCourses
select a;
List<course> lst = new List<course>();
lst = qry.ToList();
The following error occurred for the above code:
Cannot implicitly convert type
System.Collections.Generic.List<Datalogiclayer.tbcourse> to
System.Collections.Generic.List<course>
You need to somehow convert each
tbcourse
object to an instance ofcourse
. For instancecourse
could have a constructor that takes atbcourse
. You could then write the query like this:You can convert the entity object to a list directly on the call. There are methods to converting it to different data struct (list, array, dictionary, lookup, or string)
You need to use the
select new
LINQ keyword to explicitly convert yourtbcourse
entity into the custom typecourse
. Example ofselect new
:http://www.hookedonlinq.com/LINQtoSQL5MinuteOverview.ashx
No need to do so much works..
Then you can use:
It works fine for me.
What you can do is select everything into a new instance of Course, and afterwards convert them to a List.