问题
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