0

I've encountered a problem: I have a table that has a reference to another table, that has a list of references to another table, which has another list of references, and I want to eager load all of this.

Basically, the idea is:

public class User {
     public ClassL1 l1 { get; set; }
}

ClassL1 {
    public List<ClassL2> l2 { get; set; }
}

ClassL2 {
    public List<ClassL3> l3 { get; set; }
}

ClassL3 {
    //some basic string or int values
}

And currently, I'm trying to load it with something like this, but to no avail:

user = context.Users
  .Where(u => u.UserName == model.Username)
  .Include(u => u.l1)
  .ThenInclude(l1 => l1.l2)
  .ThenInclude(l2List => l2List.Select(l2Single => l2Single.l3))//This doesn't work
  //.Include(u => u.l1.l2.Select(l2Single => l2Single.l3)) //Neither does this
  .First();

How am I supposed to load a list of lists, if select isn't working? Any help would be appreciated.

EDIT: Well, since there apparently is an open bug on this, I went with a stupid fix, and made it work like this:

user = context.Users
  .Where(u => u.UserName == model.Username)
  .Include(u => u.l1)
  .ThenInclude(l1 => l1.l2)
  .First();
for (int i = 0; i < user.l1.l2.Count; i++) 
{
  user.l1.l2[i].l3 = context.l3DB
    .Where(p => p.l2.Id == user.l1.l2[i].Id)
    .ToList();
}

And for this to work, I added references up(to parents) inside the classes. for example:

public class ClassL2 {
    public List<ClassL3> l3 { get; set; }
    public ClassL1 l1 { get; set; }//reference to parent
}

2 Answers 2

1

How about this:

user = (from usr in context.Users
        where usr.UserName == model.Username
        select new
        {
            usr,
            usr.l1,
            l2List = from l2 in usr.l1.l2
                    select new
                    {
                        l2,
                        l2.l3
                    }
        }).AsEnumerable().Select(m => m.usr).First();
Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't seem to work. I get an error: "No coercion operator is defined between types 'Microsoft.Data.Entity.Query.Internal.EntityQueryable'1[ClassL3]' and 'System.Collections.Generic.List'1[ClassL3]'."
Maybe this is an issue with your mappings. Which version of EF are you using?
"EntityFramework.Commands": "7.0.0-rc1-final" "EntityFramework.Core": "7.0.0-rc1-final" I thought so too, but I can't find a way to fix it, had one prop named the same as it's classname, changed that, but the error persisted. I could upload the code snippets concerning this problem if that would be of any help.
Well it looks like there was a bug in EF 7 rc1 with selecting list navigation properties (github.com/aspnet/EntityFramework/issues/4246). Not sure if this is the same reason though. Maybe you ask the guys over there on Github. I use queries like the one I posted with EF 6.1.3 without problems, the only difference I can spot is that your properties are List<> and I usually use ICollection<>, but this should not be an issue.
Got it, will have to look into it. Thanks a lot!
0

Check this:

user = context.Users
  .Where(u => u.UserName == model.Username)
  .Include(u => u.l1)
  .ThenInclude(l1 => l1.l2)
  .ThenInclude(l2 => l2.l3)
  .First();

1 Comment

This doesn't work, l2 is a list, I cannot access properties from there, since it's not a single element. That's where my problem is at, if it worked that easily, I wouldn't have a problem, but right now i have to select first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.