Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your Address class already has a State property that represents the relation. Why don't you just use that?</p> <pre><code>public IQueryable&lt;string&gt; Addresses() { var query = from a in _contextProvider.Context.Addresses where a.CityName.StartsWith("I") select a.State.StateName; return query; } </code></pre> <hr> <p><strong>Update:</strong></p> <p>From your latest comment I guess you turned off lazy loading for your model by setting <code>LazyLoadingEnabled</code> to false. By default lazy loading is enabled which can be a bit confusing since older versions of EF didn't even support lazy loading.</p> <p>Disabling lazy loading "activates" explicit loading and unsurprisingly requires you to explicitly load related objects. In your case you can do this by either using the <code>Include</code> method (this is called eager loading):</p> <pre><code>var query = from a in _contextProvider.Context.Addresses.Include("State") where a.CityName.StartsWith("I") select a; </code></pre> <p>or access the related property in query (which modifies the final query):</p> <pre><code>var states = (from a in Addresses() // Addresses is your query method select a.State).ToList(); </code></pre> <p>In the second version linq-to-entities automatically includes the states because you are accessing them in the query. Note that your <code>Addresses</code> method returns an <code>IQueryable</code> and therefore the query will not be executed before you actually enumerate it. So the executed SQL query very much depends on how you use the query returned by <code>Addresses</code>.</p> <p>If you first execute the <code>Addresses</code> query and access the states later linq-to-entities will not include them:</p> <pre><code>var states = (from a in Addresses().ToList() // &lt;- ToList() executes the query before states are accessed select a.State).ToList(); </code></pre> <p>Lazy loading is often turned off to avoid unneeded roundtrips to the database. If you enable lazy loading and then immediately execute the query returned by <code>Addresses</code> each access to a related object will actually produce a database query. But again: if you include the states in the final query linq-to-entities will automatically include them by generating a JOIN (no matter if you use lazy loading or not).</p> <p>So your solution to use <code>Include</code> to eagerly load related states (even if you don't access them later) is ok. And your original idea to use a join is also ok. But since you want your method to return an <code>IQueryable&lt;Address&gt;</code> you have to do it on the call site (as described above).</p> <p>From my experience enabling lazy loading often makes things a lot easier. If you don't know exactly what's going on it can result in a lot of unnecessary roundtrips to the database. But you can still use <code>Include</code> to optimize your queries. To better understand how LINQ queries get translated into SQL statements I recommend to use SQL Profiler (or a free tool like <a href="http://expressprofiler.codeplex.com/" rel="nofollow noreferrer">Express Profiler</a> if you use SQL Server Express).</p> <p>On the other hand using explicit loading can also be a pain. Being forced to include all related tables can result in <a href="https://stackoverflow.com/questions/5521749/how-many-include-i-can-use-on-objectset-in-entityframework-to-retain-performance">huge datasets</a>. If for some reasons you can not include all related tables you have to explicitly check if an entity is loaded: <code>context.Entry(address).Reference(a =&gt; a.State).IsLoaded</code>. If a nullable property is null you don't know if it is NULL in the database or just not yet loaded.</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.
    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