Entity Framework include filter child collection

asp.net-mvc c# entity-framework entity-framework-6 linq

Question

I'm having some trouble adding a filter condition for objects that are included in my LINQ query. I want to know like

var item = _Context.Order.Include("Inner")
           .Include("Inner.first")
           .Include("Inner.second")
           .Where(x => ( !(x.IsDeleted) && (x.IsActive) && 
                 (x.itemid == id))).FirstOrDefault();

Another list of items is found in the code "Inner" above. I must now filter the inner objects. I simply need the inner item with the inner filter condition. true when isDeleted.

Query must return a class, such as

public class Order
{

    public string Name { get; set; }
    public List<InnerDetails> Inner{ get; set; }
    public bool IsDeleted { get; set; }
}

as well as InnerDetails class

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; }
}

Given that I am unfamiliar with LINQ and EF, can somebody recommend a better method for me to use?

1
10
4/4/2017 9:16:01 AM

Accepted Answer

ZZZ_tmp
9
4/6/2020 11:29:54 AM

Popular Answer

In EF Core, I succeeded in doing this. I'm very certain that EF6 uses the same idea.

Simplifying your illustration:

var item = ctx.Order.Include("Inner")
              .Where(x => x.Inner.Any(innerItem => innerItem.IsDeleted))
              .FirstOrDefault();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow