LINQ to Entities projection of nested list

11,129

Solution 1

You don't. You have to do this part in L2O.

So you could do:

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

There's no way to do this in one query.

Solution 2

I'm not sure what exactly your asking about, but List<T> does implement IEnumerable<T> (which is just an interface for an enumerable sequence).

A code that will do your projection and have Things be a List instead of an IEnumerable would use the ToList() operator, which creates a List<T> from any IEnumerable<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();
Share:
11,129

Related videos on Youtube

ctorx
Author by

ctorx

.NET Software Professional and Consultant

Updated on April 24, 2022

Comments

  • ctorx
    ctorx about 2 years

    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!

  • ctorx
    ctorx almost 14 years
    ToList() on a LINQ to Entities Query is not supported, which is why I have asked this question.
  • Sam
    Sam almost 14 years
    Is there some reference for this? MSDN's own LINQ to Entities shows you can in fact use ToList. msdn.microsoft.com/en-us/library/bb896246.aspx?ppud=4
  • ctorx
    ctorx almost 14 years
    You can use ToList() on the result of a query, but now within a query. In the example of your link, the ToList() is added to the end of a LINQ expression. In my code, it is embedded within the expression, hence, not supported.
  • Sam
    Sam almost 14 years
    Gotcha, the root cause is LINQ to Entities cannot instantiate the List<T>, or any IEnumerable for that matter, inside of a query. mosesofegypt.net/post/…
  • ctorx
    ctorx almost 14 years
    Thanks, I just figured this out too.