I have some difficulty to add some filter condition for included items in my LINQ query. My query is like
var item = _Context.Order.Include("Inner")
.Include("Inner.first")
.Include("Inner.second")
.Where(x => ( !(x.IsDeleted) && (x.IsActive) &&
(x.itemid == id))).FirstOrDefault();
In the above code "Inner" is another list of item. Now i need to filter inner items. I only need inner item with filter condition inner.isDeleted = true.
Query should return a class like,
public class Order
{
public string Name { get; set; }
public List<InnerDetails> Inner{ get; set; }
public bool IsDeleted { get; set; }
}
and InnerDetails class like
public class InnerDetails
{
public string Sample { get; set; }
public bool IsDeleted { get; set; }
public int firstId { get; set; }
public int secondID { get; set; }
public First first{ get; set; }
public Second second{ get; set; }
}
Can anyone suggest me a better approach to do this because i am new in LINQ and EF
Disclaimer: I'm the owner of the project Entity Framework Plus
EF+ Query IncludeFilter feature allows filtering related entities.
Note: You cannot mix Include & IncludeFilter.
Wiki: EF+ Query IncludeFilter
EDIT: Answer sub-question
Yes, under the hood, my library uses a similar solution as projection
Note: Code have not been tested
EDIT: Answer comment
Unfortunately, even the latest
EF Core
version still has issues with projection and cause N+1 queries. The feature will be available when a new version (stable or pre-release) will no longer have this issue.