Well it has been a while and I have been working with some really cool stuff lately.

I thought I would share this handy little tid bit of info to make your life easier in .NET specifically C#.

How many times have you looped through a list looking for matching class properties and added them to another list to display them? Well I know I have done it way to many times so when I started using LINQ and lambda expressions I got really excited!

Ok so without further a do lets go.

Say we have a base class

public class Person{
  public String FirstName{get;set;}
  public String LastName{get;set;}
  public String Email{get;set;}
}

Now given a method that populates a list of Person objects

var myList = new List<Person>();

for(int i = 0; i < 10; i++){
 myList.Add(
  //Excuse the concatination, prefered method is String.Format
  new Person{FirstName = "Name [1]" + 1, LastName = "LastName " + [1], Email = "Email" + i + "@domain.com"};
 )
}

Now we have a list of Person with 10 entries. Now if we wanted to find the entry with the first name "Name 1" you would traditionally loop through the list till you matched the name. Something like

var myPerson = null;
foreach(var person in myList){
  if(myList.FirstName.Equals("Name 1")){
    myPerson = person;
    break;
  }
}

Man that can get annoying. So MS has provided us with a handy set of extension method in the LINQ library. To include it in your cs file you simply reference the namespace System.Linq

using System.Linq;

Now to achieve the same thing as the foreach loop we simply go

//The first or default method returns null if it doesn't find a match, First() will throw an exception if no
//matching objects are found
var myPerson = myList.FirstOrDefault(person => person.FirstName.Equals("Name 1"));

Now that is neat!
We can also query lists from the list

//Obviously this will return all ten results but illustrates the purpose
var myNewList = myList.Where(person => person.FirstName.StartsWith("Name "));

Cool huh? But this is only the beginning! Say we add another property to person.

public class Person{
  public String FirstName{get;set;}
  public String LastName{get;set;}
  public String Email{get;set;}
  public int Salary {get;set;} //yes I know, float float float -> keeping it simple
}

and we populate it with

var myList = new List<Person>();
Random random = new Random();
int randomNumber = random.Next(0, 100);

for(int i = 0; i < 10; i++){
 myList.Add(
  //Excuse the concatination, prefered method is String.Format
  var salary = random.Next() * 1;
  new Person{FirstName = "Name [1]" + 1, LastName = "LastName " + [1], Email = "Email" + i + "@domain.com", Salary = salary};
 )
}

And we want to find the total cost of our List of Persons

var total = myList.Sum(person => person.Salary);

Or we want to find the average of all the salaries

var avg = myList.Avg(person => person.Salary);

Now I don't care who you are, that is cool.It extends far further than that so I recommend having a look at it!

For more info check out Lambda expressions:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions

Linq:

https://docs.microsoft.com/en-us/previous-versions/bb397926(v=vs.140)

Enjoy!

This page was published on It was last revised on

Contributing Authors

0

0 Comments

  • Votes
  • Oldest
  • Latest