Entity Framework include items only by condition [duplicate]

偶尔善良 提交于 2019-12-25 01:16:28

问题


I need my context to include the sonns by a condition, I need the rows that not deleted (logical delete).

I understood that I cannot add a condition to the include; so I want to filter the context, but it's not working.

var aa = ctx.aa
         .Include(t => t.vari)
         .ToList()
         .FirstOrDefault();

ctx.vari.Where(bi => bi.ID == 10 && bi.Deleted == 1).ToList();

Thanks!


回答1:


As codelahiru && hbulens pointed out, you missed the bi for ID.

Disclaimer: I'm the owner of the project Entity Framework Plus

The Query IncludeOptimized feature allows to filter with include and optimize the query performance at the same time (Support EF5, EF6)

var aa = ctx.aa
            .IncludeOptimized(t => t.vari.Where(bi => bi.ID == 10 && bi.Deleted == 1))
            .FirstOrDefault();

Documentation: EF+ Query IncludeOptimized




回答2:


This isn't going to be the most performant query. It would be better to drop the ToList()

 var aa = ctx.aa.Include(t => t.vari).ToList().FirstOrDefault(); 

 // You missed the variable before ID
 ctx.vari.Where(bi => bi.ID == 10 && bi.Deleted == 1).ToList();


来源:https://stackoverflow.com/questions/38035849/entity-framework-include-items-only-by-condition

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