1

I use EF 6.1 and have two classes as DB model:

public class A {
    public int Id { get; set; }
    // ...  
    public virtual List<B> Bs { get; set; }
}
public class B {
    public int Id { get; set; }
    public MyType Type { get; set; }
    public byte[] Data { get; set; }
    public int AId { get; set; }
    [ForeignKey("AId")]
    public virtual A A { get; set; }
}
enum MyType { x1, x2 }

How could load A entitys and their related entity B which has B.Type == x1 and use them in disconnected mode.

I want something like this (doesn't exist)

context.A.Include(a => a.Bs.Where(a => a.Type == x1))

this query load all related entity

context.A.Include(a => a.Bs)

I've tried :

context.Entry(a).Collection(a => a.Bs).Query()..Where(b => b.Type == x1).Load();

but after closing context, access to B throws exception.

any suggestion?

Edit: I also tried load each entity in separate query

var AQuery =  context.A.Where ...
var aList = AQuery.ToList();

var bList = (from b in context.B
        where b.Type == x1 && AQuery.Contains(b.A)
        select b).ToList();

but I can't manually add b to A.Bs list (because Virtual property I guess)

  • You might take a look at this question, it looks like it is similar to what you are asking: http://stackoverflow.com/questions/7079378/how-to-filter-nested-collection-entity-framework-objects – DHP May 31 '16 at 17:01

1 Answers1

0

I could achieve my desired functionality by disabling lazy load. (LazyLoadingEnabled = false)

I use below queries

var AQuery =  context.A.Where ...
var aList = AQuery.ToList();
var bList = (from b in context.B
        where b.Type == x1 && AQuery.Contains(b.A)
        select b).ToList();

aList items populated with appropriate Bs and usable in disconnected mode.