Entity Framework: EF gets related objects of an entity in different order

I'm working with MVC3 and Entity Framework 4.0. The situation is the following: I have an entity called "Topics" and a related entity called "Questions". To visualize the questions I have a Topics view and then in a foreach sentence inside the view, show the Questions related to that topic:

foreach(Questions Question in Topic.Questions) { //Show question info }

But anytime a question is updated in another view, when I go to the Topic view, the questions appear in a diferent order, most specifically, the question that was updated appears first.

Anyone knows why is this behavior?. And how to stop it?

Upvotes: 3

Views: 57

Answers (1)

Nick Butler
Nick Butler

Reputation: 24383

I assume you're using a query something like this:

context.Topics.Include( o => o.Questions )...

You can't specify the order of included tables in EF, but you can still use LINQ to objects.

So just change your foreach to:

foreach ( Question question in topic.Questions.OrderBy( ...required order... ) )

Upvotes: 1

Related Questions