C# LINQ查询列表中的列表

C# LINQ查询列表中的列表,c#,linq,linq-to-entities,C#,Linq,Linq To Entities,我有这样的情况: 我的模型视图: 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 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; }
}
当我打电话时:

subjects.ToList()我得到异常:

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.

不能在linq中调用
ToList
,以进行实体查询。Linq to entities查询将始终投影到
IEnumerable
,因此如果需要
IList
,必须在Linq to对象中调用它

试试这个:

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()
                         });

您不能也不应该在iQueryAble查询中使用
ToList()
。请注意,此查询必须转换为SQL

您的类和查询不匹配。Assistants=(来自实体中的subject1。subject1中的Subjects来自assistant。讲师,其中assistant.Professor==false选择新SSVN.ModelView.assistant(){AssistantFullName=assistant.LastName+“”+assistant.Name})我在这里遇到了问题,因为助手是IList,此查询使iQueryAbleno。您不会在linq to entities部分中投影到您的模型视图。您将改为使用匿名类型,并在linq to object部分中投影到您的模型视图,并转换为
IList
。我已设法使助手属性IQueryable和t在我看来,我只是调用了.ToList()方法。这样就可以了。:)谢谢LadislavYour,你是对的,我还没有看到你从Upper select中删除了类型。:)
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()
                         });