LINQ to Entities Projection of Nested List

Posted by Matthew on Stack Overflow See other posts from Stack Overflow or by Matthew
Published on 2010-05-28T16:53:50Z Indexed on 2010/05/28 17:02 UTC
Read the original article Hit count: 637

Assuming these objects...

class MyClass
{
     int ID {get;set;}
     string Name {get;set;}
     List<MyOtherClass> Things {get;set;}
}

class MyOtherClass
{
     int ID {get;set;}
     string Value {get;set;}
}

How do I perform a LINQ to Entities Query, using a projection like below, that will give me a List? This works fine with an IEnumerable (assuming MyClass.Things is an IEnumerable, but I need to use List)

MyClass myClass = (from MyClassTable mct in this.Context.MyClassTableSet
                        select new MyClass
                        {
                             ID = mct.ID,
                             Name = mct.Name,
                             Things = (from MyOtherClass moc in mct.Stuff
                                       where moc.IsActive
                                       select new MyOtherClass
                                       {
                                            ID = moc.ID,
                                            Value = moc.Value
                                       }).AsEnumerable()
                        }).FirstOrDefault();

Thanks in advance for the help!

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework