user2505650
user2505650

Reputation: 1381

Group elements in a List that share a common characteristic

I would like to order elements of a certain class by a certain crtieria.

For example if I have this class :

public class Contact {

public string FirstName  { get; set; }
public string LastName{ get; set; }


}

and a list of this class :

        List<Contact> ContactList = new List<Contact>
        {

            new Contact{

             FirstName  = "John" ,
             LastName =  "Smith"
            }   ,


            new Contact{
             FirstName = "Andrew" , 
                LastName = "Wallace"

            }   ,
            new Contact{
             FirstName = "Andrew" , 
                LastName = "Smith"

            }   ,   new Contact{

             FirstName  = "John" ,
             LastName =  "Wallace"
            }   ,






        };

How can i Use Linq to order my ContactList so that then it creates two lists one with Contacts whose firstname is John and the Other whose firstname is Andrew ?

This List is small but what if I have a huge list and i dont know what is inside the list and how can I order it with respect to the FirstName characteristic ?

Upvotes: 0

Views: 68

Answers (2)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101742

var result =ContactList.GroupBy(c => c.FirstName)
            .Select(c => c.ToList())
            .ToList()

That will give you a List<List<Contact>> which contains two lists.First one contains Johns and second one contains Andrews.Here is the output:

enter image description here

Update:

According to your comment you can use a dictionary

var dict = ContactList.GroupBy(c => c.FirstName)
            .Select(c => c.ToList())
            .ToDictionary(x => x.First().FirstName, x => x);

After this you can get John's list with:

dict["John"] 

Which give you a list that Contains all John's

Upvotes: 1

goodface87
goodface87

Reputation: 236

This List is small but what if I have a huge list and i dont know what is inside the list and how can I order it with respect to the FirstName characteristic ?

For ordering it on the basis of first name, you can order your list with Linq like so.

var listOrderedByName = ContactList.OrderBy(cl => cl.FirstName);

and in this example you would end up with a single list where your contacts are ordered with all the Andrew contacts first and then all the John contacts after. This would work with a huge list as well.

How can i Use Linq to order my ContactList so that then it creates two lists one with Contacts whose firstname is John and the Other whose firstname is Andrew ?

If you happen to know the specifics of your contact list and you just want one list with Johns and one list with Andrews you can do that with Linq like so.

var listWithJohnContactsOnly = ContactList.Where(cl => cl.FirstName == "John");
var listWithAndrewContactsOnly = ContactList.Where(cl => cl.FirstName == "Andrew");

This would give you two separate lists. One with just contacts with the first name "John" and the other with contacts with the first name "Andrew". If you were hoping to group them and end up with a list of lists (each list containing contacts with a particular first name) then you can use GroupBy like Selman22 suggested, and you wouldn't have to know about the specifics of the inside of your list.

Upvotes: 0

Related Questions