How does LINQ expression syntax work with Include() for eager loading

后端 未结 5 1520
天命终不由人
天命终不由人 2020-11-30 05:55

I have a query below, but I want to perform an Include() to eager load properties. Actions has a navigation property, User (Action.User)

1) My basic query:

5条回答
  •  鱼传尺愫
    2020-11-30 06:07

    Doing the basic query mentioned in your posted question you won't be able to see the User properties unless you return an anonymous type as following:

    from a in Actions
    join u in Users on a.UserId equals u.UserId
    select new
    {
       actionUserId = a.UserId
       .
       .
       .
       userProperty1 = u.UserId
    };
    

    However to use the Include method on the ObjectContext you could use the following:

    Make sure you have LazyLoading off by using the following line:

    entities.ContextOptions.LazyLoadingEnabled = false;
    

    Then proceed by

    var bar = entities.Actions.Include("User");
    var foo = (from a in bar
               select a);
    

提交回复
热议问题