Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder by a field which is a Navigation Property to an Entity - Linq to Entity
    primarykey
    data
    text
    <p>I've got a scenario where I will need to order by on a column which is a navigation property for the Users entity inside my EF model.</p> <p>The entities: Users --> Countries 1:n relationship</p> <p>A simple SQL query would be as follows:</p> <pre><code>SELECT UserId, u.Name, c.Name FROM users u join countries c on u.CountryId = c.CountryId ORDER BY c.Name asc; </code></pre> <p>So then I tried to replicate the above SQL query using Linq to Entities as follows - (Lazy Loading is enabled)</p> <pre><code>entities.users.OrderBy(field =&gt; field.country.Name).ToList(); </code></pre> <p>But this query does not return my countries sorted by their name as the native SQL query above does.</p> <p>However I continued a bit more and did the following:</p> <pre><code>var enumeratedUsers = entities.users.AsEnumerable(); users = enumeratedUsers.OrderBy(fields =&gt; fields.country.Name).ToList(); </code></pre> <p>But ordering on the enumeratedUser object for about 50 records took approx. 7seconds</p> <p>Is there a better way how to omit the Enumerable and without returning an anonymous type?</p> <p>Thanks</p> <p><strong>EDIT</strong></p> <p>I just forgot to say that the EF provider is a MySQL one not a MS SQL. In fact I just tried the same query on a replicated database in MS SQL and the query works fine i.e. the country name is ordered correctly, so it looks like I have no other option apart from getting the result set from MySQL and execute the order by from the memory on the enumerable object</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