none
Linq grouping .include(x=>x.Entity) returning null Entity RRS feed

  • Question

  •    var packageGroupBy = _repository
                                  .GetQuery<PackageSendHistory>()
                                  .Where(p => p.PackageSendFormatID == (int)PackageSendFormatEnum.USPS)
                                   .Include(x => x.Package)
                                   .GroupBy(p => new
                                   {
                                       p.Package.Title,
                                       p.Package.Name,
                                   });
    I have a linq query above which is grouping by Title and Name. Im trying to include Package entity but its not loading as a part of query. It remains null when i see in the result
    • Edited by lax4u Thursday, September 27, 2012 9:20 PM
    Thursday, September 27, 2012 9:20 PM

Answers

  • Hi lax4u;

    looking at your last post I saw nothing that should have not returned the included path in the query. After looking at the documentation I read this. 

    Microsoft Documentation : DbExtensions.Include<T, TProperty>
    "When you call the Include method, the query path is only valid on the returned instance of the IQueryable of T. Other instances of IQueryable of T and the context itself are not affected. You can call this method multiple times on an IQueryable of T to specify multiple paths for the query."

    A query returning a IGrouping<TEntity> does not qualify and therefore the path is not valid and does not return the Includes.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Alexander Sun Tuesday, October 9, 2012 9:10 AM
    Sunday, October 7, 2012 2:53 PM

All replies

  • Hi lax4u;

    By the looks of it GetQuery<PackageSendHistory>() is returning a collection of PackageSendHistory and therefore it looks like the Linq to Entity Framework query has already been executed and the rest of the query in the question is executing on the local objects and not hitting the database.

    Please show the GetQuery method.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, September 28, 2012 1:46 PM
  • GetQuery() returns IQueryable<PackageSendHistory> not collection..
    Monday, October 1, 2012 3:26 PM
  • If you can show the method GetQuery() it will help.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, October 1, 2012 4:55 PM
  • public class GenericRepository : IGenericRepository { private IObjectContext _objectContext; public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class { return _objectContext.CreateObjectSet<TEntity>(); } public IQueryable<TEntity> GetQuery<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class { return GetQuery<TEntity>().Where(predicate); } }

    public sealed class ObjectContextAdapter : IObjectContext { private readonly System.Data.Objects.ObjectContext _context; public ObjectContextAdapter(System.Data.Objects.ObjectContext context) { _context = context; } public ObjectSet<TEntity> CreateObjectSet<TEntity>() where TEntity : class { return _context.CreateObjectSet<TEntity>(); } }




    • Edited by lax4u Thursday, October 4, 2012 7:55 PM
    Thursday, October 4, 2012 7:54 PM
  • Hi lax4u;

    looking at your last post I saw nothing that should have not returned the included path in the query. After looking at the documentation I read this. 

    Microsoft Documentation : DbExtensions.Include<T, TProperty>
    "When you call the Include method, the query path is only valid on the returned instance of the IQueryable of T. Other instances of IQueryable of T and the context itself are not affected. You can call this method multiple times on an IQueryable of T to specify multiple paths for the query."

    A query returning a IGrouping<TEntity> does not qualify and therefore the path is not valid and does not return the Includes.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Alexander Sun Tuesday, October 9, 2012 9:10 AM
    Sunday, October 7, 2012 2:53 PM