1

I need a push in the right direction with regards to ordering data using Linq.

I've got a list of People objects in C#, which in turn, holds a list of Sites that the person may be associated with.

Those Site objects holds is an instance of an Organisation object, that the site is associated with (as an organisation may have many sites..)

Edit: A person will only belong to a single organisation in the list. (Thanks @jonskeet)

How do I order the list of people, using Linq (or Lamba) to show them in alphabetical order, by their Organisation, and then ordered by the Surname, Firstname of the contact..??

3
  • Your requirements are unclear - what if a person belongs to more than one organization? Which should be used for sorting? Commented Aug 4, 2011 at 14:03
  • Ah, beg your pardon. The person will only belong to one organisation. Commented Aug 4, 2011 at 14:04
  • So why does a Person have a list of sites? It seems that the organization should be set on the person, not on each of the sites... Commented Aug 4, 2011 at 14:08

3 Answers 3

3

It strikes me that your model is a bit messed up, but if you're sure that:

  • A person will have at least one site
  • All the sites for a particular person will be for the same organization

you can use:

var sorted = people.OrderBy(p => p.Sites.First().Organization)
                   .ThenBy(p => p.LastName)
                   .ThenBy(p => p.FirstName);
Sign up to request clarification or add additional context in comments.

4 Comments

It is a bit messed up, but that's another story! :-/
Close enough! Here's what I've had to use (for anyone else's reference):
sorted = people.OrderBy(x => x.Sites.FirstOrDefault().Organisation.Name).ThenBy(x => x.Surname).ThenBy(x => x.Firstname);
@Brett: You shouldn't use FirstOrDefault - that implies there could be an empty list of sites, in which case you'll end up with a null reference which will still go bang, but in a less obvious way.
0
people.SelectMany(p => p.Sites).OrderBy(s => s.Organisation.Name).ThenBy(s => s.Organisation.Contact.Surname).ThenBy(s => s.Organisation.Contact.FirstName);

1 Comment

Thanks for this, but there's a list of Site objects in between the person and the organisation...
0

Linq supports an order by clause. Check out this MSDN link

http://msdn.microsoft.com/en-us/library/bb383982.aspx

Example

public class CityLookup
{       public string City { get; set; }    
    public string Country { get; set; }
}

List<CityLookup> citiesLkp =            
new List<CityLookup>            
{       
    new CityLookup{ City = "New Delhi", Country = "India" },          
    new CityLookup{ City = "Sydney", Country = "Australia" },  
    new CityLookup{ City = "Tokyo", Country = "Japan" },                 
    new CityLookup{ City = "New York", Country = "USA" },                
    new CityLookup{ City = "Paris", Country = "France" },                
    new CityLookup{ City = "Barcelona", Country = "Spain" },                                          
};


// Now sort the names based on city name
var myList =    from c in citiesLkp    orderby c.City    select c;

foreach (var record in myList )    
{        
    Console.WriteLine(record.City);    
}

1 Comment

Is there anything else you can add? A code sample, maybe? Generally answers here are expected to be self-contained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.