Entity framework filter lambda navigation properties

别来无恙 提交于 2019-12-13 04:37:31

问题


Using .NET Entity Framework 6 I need to filter the elements of an included virtual collection. What I mean is easily explained with the following code:

context.MyEntity.Include( navigationPropertyCollection => navigationPropertyCollection.Where( np => np.IsActive() ) )

the code code is just an example, to say from MyEntity I want include only active elements of navigationPropertyCollection.

Is there a smart way to do it?


回答1:


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

msdn reference

you could try this by anonymous projection

var resultObjectList = _context.
                   Parents.
                   Where(p => p.DeletedDate == null).
                   OrderBy(p => p.Name).
                   Select(p => new
                             {
                                 ParentItem = p,
                                 ChildItems = p.Children.Where(c => c.Name=="SampleName")
                             }).ToList();

Similar Answer in Stack



来源:https://stackoverflow.com/questions/33801565/entity-framework-filter-lambda-navigation-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!