Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Querying list in list

I have this situation:

My ModelView:

public class Subject
{
   public int ID { get; set; }
   public string Name { get; set; }
   public int ProfessorID { get; set; }
   public string ProfessorFullName{ get; set; }
   public IList<Assistant> Assistants { get; set; }
}

public class Assistant
{
   public string AssistantFullName{ get; set; }
}

My query:

var subjects = from subject in Entities.Subjects
                            from professor in subject.Lecturers
                            where professor.Professor == true
                            select new SSVN.ModelView.Subject()
                            {
                                ID = subject.ID,
                                Name= subject.Name,
                                ProfessorFullName= professor.LastName+ " " + professor.Name,
                                Assistants= (from subject1 in Entities.Subjects
                                            from assistant in subject1.Lecturers
                                            where assistant.Professor == false
                                            select new SSVN.ModelView.Assistant()
                                            {
                                                AssistantFullName = assistant.LastName+ " " + assistant.Name
                                            }).ToList()
                            };

And when I call:

subjects.ToList(); I get exception:

LINQ to Entities does not recognize the method
'System.Collections.Generic.List`1[SSVN.ModelView.Assistant] ToList[Assistant]
(System.Collections.Generic.IEnumerable`1[SSVN.ModelView.Assistant])' method, and this 
method cannot be translated into a store expression.
like image 440
Vajda Avatar asked Dec 10 '25 09:12

Vajda


1 Answers

You cannot call ToList inside linq-to-entities query. Linq-to-entities query will always project to IEnumerable<T> so if you want IList<T> you must call it in linq-to-objects.

Try this:

var subjects = (from subject in Entities.Subjects
                from professor in subject.Lecturers
                where professor.Professor == true
                select new 
                    {
                        ID = subject.ID,
                        Name= subject.Name,
                        ProfessorFullName= professor.LastName+ " " + professor.Name,
                        Assistants= (from subject1 in Entities.Subjects
                                     from assistant in subject1.Lecturers
                                     where assistant.Professor == false
                                     select new SSVN.ModelView.Assistant()
                                         {
                                             AssistantFullName = assistant.LastName+ " " + assistant.Name
                                         })
                    }).AsEnumerable().Select(x => new SSVN.ModelView.Subject
                         {
                            ID = x.ID,
                            Name = x.Name,
                            ProfessorFullName = X.ProffesorFullName,
                            Assistants = x.Assistants.ToList()
                         });
like image 86
Ladislav Mrnka Avatar answered Dec 12 '25 21:12

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!