Basics of LINQ II

After a very simple beginning of LINQ let’s continue to play with it more.

In this article, I will try to share some basic methods such as First,Last, Max and Min.

1. Numbers Example:

Let’s create an int array with some numbers and find the first number, last number, first number which is greater then 4, first odd number and number less then 6.

var numbers = new int[] { 2, 3, 4, 5, 6, 7, 8, 9 };

 var firstNumber = numbers.First(); // = 2

 var lastNumber = numbers.Last(); // = 9 

 var firstNumberGreaterThenFour = numbers.First(t => t > 4) ; // = 5

 var firstOddNumber = numbers.First(n => n % 2 == 1); // = 3

 var lastNumberLessThanSix = numbers.Last(n => n < 6); // = 5

2. My Friends

In this part, I’ve created a custom class called Friend and made a list of my friend with their age.

 public class Friend
 {
 public Friend(string name, int age) 
 {
 this.name = name;
 this.age = age; 
 }
 public string name { get; set; }
 public int age {get;set;}
 }

Next added my friends to a list.

List<Friend> myFriends = new List<Friend>()
 {
 new Friend("Jose Angel", 45), 
 new Friend("Luisa", 39),
 new Friend("Begonya", 25),
 new Friend("Koji", 73),
 new Friend("Cris", 23),
 new Friend("Lars", 64),
 new Friend("İsmail", 54),
 new Friend("Pedro", 44),
 new Friend("Manual", 33),
 new Friend("Elena", 21),
 new Friend("Angelica", 24),
 new Friend("Rene", 32),
 new Friend("Pajarita", 4),
 new Friend("Lazarillo", 4),
 new Friend("Laura", 66)
 };

After these steps, I made a simple report of my friends with their ages:

  1. Number of friends I have
  2. Number of friends older then me
  3. Number of friends younger then me
  4. Age of my youngest friend
  5. Age of my oldest friend
  6. List of my friends ordered by their age asc
  7. Names of my friends whose age is 4
  8. Names of my friends older than me
  9. Name of my oldest friend
  10. Sum of ages of my friends
  11. Average of ages of my friends
 Console.WriteLine("1. Number of friends I have: " + myFriends.Count() + "\n");
 Console.WriteLine("2. Number of friends older then me : " + myFriends.Where(n => n.age > 28).Count() + "\n");
 Console.WriteLine("3. Number of friends younger then me : " + myFriends.Where(n => n.age < 28).Count() + "\n"); Console.WriteLine("3. Age of my youngest friend :" + myFriends.Min(n => n.age) + "\n");
 Console.WriteLine("5. Age of my oldest friend :" + myFriends.Max(n => n.age) + "\n");
 
 Console.WriteLine("-----------------------------------------");
 
 Console.WriteLine("6. List of my friends ordered by their age asc:");

 foreach (var friend in myFriends.OrderBy(n => n.age))
 {
 Console.WriteLine(friend.name + " " + friend.age.ToString());
 }

 Console.WriteLine("-----------------------------------------");

 
 Console.WriteLine("7. Name of my friends whose age is 4:");
 IEnumerable<string> myYoungestFriend = from i in myFriends
 where i.age == 4
 select i.name;
 foreach (var myLittleFriend in myYoungestFriend)
 {
 Console.WriteLine(myLittleFriend);
 }
 Console.WriteLine("-----------------------------------------");

 
 Console.WriteLine("8. Name of my friends older than me: ");
 IEnumerable<Friend> myFriendsOlderThanMe = from i in myFriends
 where i.age > 28
 select i ;

 foreach (var oldFriend in myFriendsOlderThanMe)
 {
 Console.WriteLine(oldFriend.name + " " + oldFriend.age);
 }

 Console.WriteLine("9. Name of my oldest friend: ");
 Friend myOldestFriend = myFriendsOlderThanMe.Where(o => o.age == myFriendsOlderThanMe.Max(x => x.age)).FirstOrDefault();


 Console.WriteLine(myOldestFriend.name + " " + myOldestFriend.age);

 Console.WriteLine("-----------------------------------------");

 Console.WriteLine("10. Sum of ages of my friends: ");

 var sumOfAges = myFriends.Sum(n => n.age);
 Console.WriteLine(sumOfAges +" years of life. So many! 🙂 ");

 Console.WriteLine("11. Average of ages of my friends:");
 var averageOfAges = myFriends.Average(n => n.age);
 Console.WriteLine(averageOfAges);

Some basic methods of LINQ are all. The queries written here was all Fluent Style of LINQ. You may also use Query Expression Style  which is a bit different but operates same at the end.

Feel free to contribute and comment.

Advertisements
This entry was posted in LINQ and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s