Can't get results from a IQueryable.

Posted by StackPointer on Stack Overflow See other posts from Stack Overflow or by StackPointer
Published on 2010-06-14T21:56:59Z Indexed on 2010/06/14 22:02 UTC
Read the original article Hit count: 278

Filed under:
|

Hi!

I have the following code in Linq to Entity:

var policy= from pol in conn.Policy
            where pol.Product.DESCRIPTION=="someProduct"
            SELECT pol;

Then, the table Policy, has some dependencies for a table called Entity. If I do this:

foreach(Policy p in policy){
    if(!p.Entity.IsLoaded) p.Entity.Load();
    IEnumerable<Entity> entities= from ent in p.Entity
                                  Where ent.EntityType.DESCRIPTION=="SomeEntityType"
                                  select ent;
    Console.Writeline(entities.ElementAt(0).NAME);
}

It says, "Object not set to an instance", but if I do:

foreach(Policy p in policy){
    if(!p.Entity.IsLoaded) p.Entity.Load();

    foreach(Entity et in p.Entity)Console.Write(et.NAME);

}

It works! Can anyone tell me why?

Thank you, Best regards.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-entities