EF 6 filtering child collections

后端 未结 3 1209
臣服心动
臣服心动 2021-01-04 11:46

I\'m trying to migrate old project from Linq2Sql to EF6 and I got following issue.

This project is multilingual (i.e. all texts have more than 1 translation) and I h

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 12:16

    Note that it is not currently possible to filter which related entities are loaded. Include will always bring in all related entities.

    Msdn Reference

    var result = dc.ExampleEntity1.Include(ee =>ee.TextEntry.LocalizedContents)
                   .Select(x=>new
                   {
                      //Try anonymous or a projection to your model.
                      //As this Select is IQuerable Extension it will execute in the data store and only retrieve filtered data.
                      exampleEntity = x,
                      localizedContetnt = x.TextEntry.LocalizedContents.Where(g=>g.Id==YourKey),
                   }).FirstOrDefault();   
    

    You could try anonymous projection to filter contents in the Included entities

    Entity framework team is working on this you could cast your vote here

    Similar Answer

提交回复
热议问题