Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework join statement in a function
    primarykey
    data
    text
    <p>I'm trying to create a function to grab a List&lt;>() of type "tableContact" which is an entity (sort of like when you do a simple select statement: query.ToList();)</p> <p>But this is becoming frustrating because I kept getting "null object reference" errors. For some reason "join" statement doesn't work, so I tried an alternate linq statement.</p> <p>I have an Object2Contacts table that I want to LEFT JOIN and I'm looking up my Object and trying to see all the contacts for this Object. I also can't figure out how to change an "anonymous type" back into an entity table.</p> <p>Also not every object has contacts, so sometimes null List&lt;> should be returned.</p> <pre><code>public List&lt;tableContact&gt; getContactsForObject(int oid) { if (oid &gt; 0) { var query = (from s in entities.tableContacts from o in entities.tableObject2Contacts where s.contact_id == o.contact_id where o.object_id == oid select new { s }); if (query != null) { IEnumerable&lt;tableContact&gt; e = (IEnumerable&lt;tableContact&gt;)query.ToList(); return (List&lt;tableContact&gt;)e; } } return new List&lt;tableContact&gt;(); } </code></pre> <p>I want to then loop through the object returned... e.g.:</p> <pre><code>foreach ( tableContact c in MyList){ WriteLine(c.Name); } </code></pre> <p><strong>EDIT</strong> I also tried:</p> <pre><code>List&lt;tableContacts&gt; contacts = (from s in entities.tableContacts join o in entities.tableObject2Contacts on s.contact_id equals o.contact_id where o.object_id == oid select s).ToList(); </code></pre> <p>Then I can't convert it back into a List and still "null reference".</p> <p><strong>EDIT 2</strong> Yes the query I am running may definitely bring in an "empty" list. I don't mind empty lists, but it shouldn't give "object null reference."</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload