views:

203

answers:

3

Hello,

I have a many-to-many relationship between 2 entities in Entity Framework, like here . So, Employees and Projects. At one point, I would like to insert some Projects to a Employees entity in a specific order. By conserving the order I would like to know which was the first preference of the Employees for a Projects entity. The thing is that although I order the Student.Projectslist in the way I like before the insert, when selecting Employees.Projects.FirstOrDefault(), the entities are ordered after the ProjectsId and I don't get the first element I inserted. How can I conserve the order I want?

O course, I could make a new field PreferredProjects and save the other Projects in a random order, since only the preferred one is important for me. But this is not an option, being given the context of the current project's software design.

Thank you in advance...

A: 

Try Employees.Projects.OrderBy(x => x).FirstOrDefault()

k0ni
+1  A: 

It sounds like you simply want to have sorted child collection results when you do a query, rather than take full control of the insert order.

You can achieve that using the techniques described in Tip 1 of my tips series.

Hope this helps.

Alex

Program Manager Entity Framework Team.

Alex James
A: 

Unfortunately, there is no easy solution. I have the same problem. The only solution is to save after adding each child item (project). This is the only way to save the order without using a new field column to sort input.

osa