c# - Getting List of Model1 with List of Model2 in it LINQ - MVC 4 EntityFramework 5 -
I have a requirement where I need to get a list of model 1 (list) using Linq, Model2 in Model2 Is in the list) and I also need to bring this also. For this I have created a link, but I am getting the following error:
LINQ does not recognize the units 'system'. Colections. Generics.list
1 [OurCourse] ToList [OurCourse] (System.Collections.Generic.IEnumerable 1 [OurCourse]) method, and this method can not be translated into a store expression.
Please see below for details:
I have two table colleges and courses, which have the following columns: college: ID, name,
My project is two visual models for them, contact, city, address
Cource: ID, CollegeID, name, year
As follows:
public class OurCollege {public string name {get; Set; } Public String Contacts {get; Set; } Public listing & lt; OurCourse & gt; Micors {get; Set; }} Public class OurCourse {public string name {get; Set; } Public integer number = (received); Set; }}
Here is the query query that I have prepared but I am getting the error:
var colleges = db Colleges Select (cld = & gt; New Our College () {name = cld.Name, contact = cld.Contact, MyCourses = cld.Course. Select (crs = & new; NewCourse () {name = crs.Name, NumberOfYears = Crs.Years}). ToList ()}). ToList ();
How to do this:
from MyCourses = CRD in CRD. Choose Course New Our Course {name = crs.Name, NumberOfyears = crs.Years}
Your full query will now be shown:
var colleges = db .Colleges. Selection (cld => New Our College () {name = cld.Name, contact = cld.Contact, MyCourses = (From CRD in CRD. Course new select our course {name = crs.Name, NumberOfyears = crs. Years}) toList ()}) ToList ();
Actually LINQ to Entities converts your query into SQL. It is not known that
SQL in
ToList () is an optional
list & lt; T & gt; to
IEnumerable & lt; T & gt; and
remove ToList () Use your original code:
Public IEnumerable & lt; OurCourse & gt; Micors {get; Set; }
and in the query:
var colleges = db College. Select (cld = & new; New OurCollege () {name = cld.Name, contact = cld.Contact, MyCourses = cld.Course. (CRS => Our NewCourse () {name = crs.Name, NumberOfyears = crs .Years})}). ToList ();
For more information go to
Comments
Post a Comment