LINQ to Entities nested list projection

c# entity entity-framework linq-to-entities

Question

Considering these things...

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 can I run a LINQ to Entities query that returns a List using a projection like the one below? With an IEnumerable, this works properly (presuming MyClass). Although Things is an IEnumerable, I must use a 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 your assistance!

1
8
6/23/2010 10:39:14 PM

Accepted Answer

Not you. This portion must be completed in L2O.

Thus, you could:

var q = (from MyClassTable mct in this.Context.MyClassTableSet
         select new // note anonymous type; important!
         {
             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();

MyClass myClass = (from mct in q
                   select new MyClass
                   {                 
                       ID = mct.ID,
                       Name = mct.Name,
                       Things = mct.Things.ToList()
                   }).FirstOrDefault();

This cannot be accomplished in a single query.

14
5/28/2010 5:17:58 PM

Popular Answer

I'm not sure precisely what you're requesting, butList<T> does it workIEnumerable<T> This only serves as an interface for an enumerable sequence.

The ToList() operator, which generates a list instead of an IEnumerable, may be used in code to do your projection and make Things a list instead of an IEnumerable.List<T> the anyIEnumerable<T> this:

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow