7

I keep getting errors like

: The property expression 'met => {from TrM x in met select [x].Tm}' is not valid. The expression should represent a property access: 't => t.MyProperty'.

I have a class structure of

 public class Tr: BaseModel
{
    public int Id{ get; set; }

    public List<Trm> Mets { get; set; } = new List<Trm>();

    [JsonIgnore]
    public Test TestDef { get; set; }
}



    public class Trm: BaseModel
{

    public Tm tm { get; set; }
}


public class Tm: BaseModel
{

    [JsonIgnore]
    public T TestDef { get; set; }
}

I want to be able to say when loading Tr load all Trm and include Tm when loading.

I have tried the following

 var results = await _dbContext.Tr
                .Include(tr => tr.Mets ).ThenInclude(met => met.Select(x=> x.tm))
                .Include(tr => tr.TestDef)
                .AsNoTracking()
                .ToListAsync();
            return results;

How would I do this?

Thanks

Lemex
  • 3,646
  • 14
  • 47
  • 82

1 Answers1

6

You can't use Select for Include in Ef Core. You should drill down to load related data by using ThenInclude.

   var results = await _dbContext.Tr
    .Include(tr => tr.Mets )
         .ThenInclude(met => met.tm)
    .Include(tr => tr.TestDef)
    .AsNoTracking()
    .ToListAsync();

Here is the offical documentation.

lucky
  • 11,548
  • 4
  • 18
  • 35
  • 6
    I actually discovered this but there is a bug in my version of the Rosyln complier which doesn't actually auto complete the properties for the .ThenInclude so it through me off :( :( – Lemex Jan 25 '18 at 18:47
  • the above way ddid not work from, insted I used .ThenInclude(navigationPropertyPath: tr =>tr.TestDef) – Code First Apr 01 '20 at 21:54
  • @LmC you are right but this only appears when you use same aliases in Where/Include/ThenInclude. Using different aliases brings correct property names suggestions for me. – shashwat Jul 13 '20 at 06:18