0

From what I have read the code I have should not be throwing an exception when no results are found. I am using a generic repository similar to what is found here: http://huyrua.wordpress.com/2010/07/13/entity-framework-4-poco-repository-and-specification-pattern/

The code where the exception is thrown:

List<CADDrawing> projectDrawings = repository.Find<CADDrawing>(x => x.ProjectNumber == result.StringResult)
                .Where(y => y.Obsolete == false)
                .ToList();

Does anyone have experience with this or know what would cause an exception to be thrown when running a query?

Update:

The find code:

        public IEnumerable<TEntity> Find<TEntity>(ISpecification<TEntity> criteria) where TEntity : class
    {
        return criteria.SatisfyingEntitiesFrom(GetQuery<TEntity>()).AsEnumerable();
    }

        public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
    {
        /* 
         * From CTP4, I could always safely call this to return an IQueryable on DbContext 
         * then performed any with it without any problem:
         */
        // return DbContext.Set<TEntity>();

        /*
         * but with 4.1 release, when I call GetQuery<TEntity>().AsEnumerable(), there is an exception:
         * ... System.ObjectDisposedException : The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
         */

        // here is a work around: 
        // - cast DbContext to IObjectContextAdapter then get ObjectContext from it
        // - call CreateQuery<TEntity>(entityName) method on the ObjectContext
        // - perform querying on the returning IQueryable, and it works!
        var entityName = GetEntityName<TEntity>();

        IQueryable<TEntity> ThisQuery = ((IObjectContextAdapter)DbContext).ObjectContext.CreateQuery<TEntity>(entityName);
        System.Data.Entity.Core.Objects.ObjectQuery objectQuery = (System.Data.Entity.Core.Objects.ObjectQuery)ThisQuery;
        objectQuery.MergeOption = System.Data.Entity.Core.Objects.MergeOption.OverwriteChanges;

        return ThisQuery;
    }


    public IQueryable<TEntity> SatisfyingEntitiesFrom(IQueryable<TEntity> query)
    {
        return query.Where(Predicate);
    }
Dru
  • 357
  • 4
  • 13
  • 1
    "an exception" - please elaborate. And show the `Find` code. This can be anything. – Gert Arnold Jun 07 '14 at 07:03
  • One way that I see that might solve it is to change return query.Where(Predicate); to return query.Where(Predicate).DefaultIfEmpty(new TEntity()); Not sure if that would actually work though. I can not get the exception or compile as I only have access to the code at the moment and not the database or VS – Dru Jun 07 '14 at 17:22
  • I don't believe this. You go through hoops and loops to ultimately do nothing but `IQUeryable.Where`. Your real problem is not the exception, but the hacky code. And the problem underlying that is problems with context lifespan management. First solve the lifespan issue. How does this code fit in the rest of the application? Contexts are supposed to live for the time of one (business) transaction or use case. – Gert Arnold Jun 07 '14 at 19:24
  • Yes hacky, but I did like how it worked when I first started using the code. There is definitely lifespan issues that I have run into. The application this is used in allows for the user to have a gridview of all information and be able to CRUD it for the entire lifetime of application which is basically just that gridview. Should I be re-loading/using a different context every time the user interacts with this grid? Currently with this code the context lifetime matches that of the grid. – Dru Jun 08 '14 at 01:54
  • Also, even if I get rid of this code I still end up with a .Where statement at the end of the day for the query. So wouldn't I need to follow something similar to what 3dd was suggesting even if I did not use this library? – Dru Jun 08 '14 at 02:11

1 Answers1

2

You're calling ToList() on a null object

Please see the following post for handling of null lists

how do I treat null lists like empty lists in linq?

Community
  • 1
  • 1
3dd
  • 2,520
  • 13
  • 20
  • Interesting, I was just reading how my code "should" be returning an empty list for this scenario. Your link shows a different story however. If I understand the link correctly I just need to add the .SelectMany to my code? – Dru Jun 07 '14 at 06:19
  • http://jaysmith.us/post/LINQ-Exception-Null-or-Object.aspx var whereDefaultIfEmpty = items.Where(x => x.Id == 10) .DefaultIfEmpty(new Item()); This seems like a slightly clearer solution? It looks to achieve the same result but to me looks better? – Dru Jun 07 '14 at 06:36
  • @Dru indeed it does look better, and achieves the same – 3dd Jun 07 '14 at 06:40