4

Possible Duplicate:
EF4 LINQ Ordering Parent and all child collections with Eager Loading (.Include())

Hello everybody

I have a user entity who contains a one to many relationship with a role entity

So with this linq expression :

from user in USER_TABLE.Include("USERROLE_TABLE")
order by user.Name
select user

I can get users with related roles as a child. My problem is that i want to get roles of each user ordered alphabetically. How can i do that ? I googled a lot and don't find anything

Thank's by advance !

Community
  • 1
  • 1
eka808
  • 2,257
  • 3
  • 29
  • 41

1 Answers1

1

So, just to clarify, you have a table which includes essentially:

Name : Role
--------------
Bob : leader
Jane : scribe
Bob : technician
Bob : programmer
Jane : entity
Bob : adept

and you want to end up with:

Bob : adept
Bob : leader
Bob : programmer
Bob : technician
Jane : entity
Jane : scribe

If that's the case, then you're looking at an "orderby, thenby", which in LINQ I believe is indicated by a comma in that part of the LINQ statement:

orderby user.Name, user.Role

Is that what you're looking for?

David Hagan
  • 155
  • 7
  • no i have two tables, is it working for me ? – eka808 Jan 11 '11 at 16:21
  • But after including, you've essentially got one collection, which you can order however you like. Is the output that I've proposed what you're intending to end up with - that you're hoping to order by one criteria (name), with results within that criteria ordered by a subsequent criteria (role)? Or do you want to end up with a scenario where you have a collection of (users) with no duplications, in which each user has a primary criteria (roles) field which contains a list of secondary criteria (role), containing none to many roles? – David Hagan Jan 11 '11 at 21:19