.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Lamda Expressions with multiple operations of Select(), Where(), OrderBy() and GroupBy()

Posted By:Sathakkathul Haq       Posted Date: October 07, 2010    Points: 50    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

Expression is used to remove the duplicate records with sorting order. In this article I will show how to do multiple operations on lamda expression by using various operation without duplicate data.
 

In this article I will show how to do multiple operations on lamda expression by using various operation without duplicate data.

To more about Where(), GroupBy(), Select(), OrderBy()

 

 

 

 

  

   

Let us say we have list of manufacturers and we want to order the manufacturer names without any duplicate records.

Manufacturere lists are follows,

 

    public class CarManufacturer
    {
        public string Name { get; set; }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            List list = new List();
            list.Add(new CarManufacturer() { Name = "Ford" });
            list.Add(new CarManufacturer() { Name = "Aston Martin" });
            list.Add(new CarManufacturer() { Name = "Audi" });
            list.Add(new CarManufacturer() { Name = "Bentley" });
            list.Add(new CarManufacturer() { Name = "BMW" });
            list.Add(new CarManufacturer() { Name = "Ferrari" });
            list.Add(new CarManufacturer() { Name = "Ford" });
            list.Add(new CarManufacturer() { Name = "Fuji" });
            list.Add(new CarManufacturer() { Name = "Hyundai" });
            list.Add(new CarManufacturer() { Name = "Ferrari" });
        }
    }

Now, if we print this

 Fig 1:

In Fig 1: we can find all the list of strings like Manufacturer name.

By using the Where and OrderBy trying to get manufacturer name with duplicate records.

 

List modelWithDuplicateInOrder = list.Where(m => m.Name != string.Empty).OrderBy(m => m.Name).ToList();

            Console.WriteLine("====  CarManufacturer with Duplicates =======");


            foreach (var v in list)
            {
                Console.WriteLine(v.Name);
            }

   Fig 2:

 

In Fig 2 showing the manufacturer name with duplicate records.

Now,we can see the manufacturer name without any duplicate records by using Select

 

 Console.WriteLine("==== CarManufacturer without Duplicates in order =======");

            List modelWithoutDuplicateInOrder = list.Where(m => m.Name != string.Empty).GroupBy(m => m.Name).Select(m => m.First()).OrderBy(m => m.Name).ToList();

            foreach (var v in modelWithoutDuplicateInOrder)
            {
                Console.WriteLine(v.Name);
            }

 Fig 3:

 In Fig 3 showing the manufacturer name without any duplicate records.

 Here,now we see the complete code without any duplicate records in ascending order.

   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    public class CarManufacturer
    {
        public string Name { get; set; }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            List list = new List();
            list.Add(new CarManufacturer() { Name = "Ford" });
            list.Add(new CarManufacturer() { Name = "Aston Martin" });
            list.Add(new CarManufacturer() { Name = "Audi" });
            list.Add(new CarManufacturer() { Name = "Bentley" });
            list.Add(new CarManufacturer() { Name = "BMW" });
            list.Add(new CarManufacturer() { Name = "Ferrari" });
            list.Add(new CarManufacturer() { Name = "Ford" });
            list.Add(new CarManufacturer() { Name = "Fuji" });
            list.Add(new CarManufacturer() { Name = "Hyundai" });
            list.Add(new CarManufacturer() { Name = "Ferrari" });

            List modelWithDuplicateInOrder = list.Where(m => m.Name != string.Empty).OrderBy(m => m.Name).ToList();

            Console.WriteLine("====  CarManufacturer with Duplicates =======");


            foreach (var v in list)
            {
                Console.WriteLine(v.Name);
            }


            Console.WriteLine("==== CarManufacturer without Duplicates in order =======");

            List modelWithoutDuplicateInOrder = list.Where(m => m.Name != string.Empty).GroupBy(m => m.Name).Select(m => m.First()).OrderBy(m => m.Name).ToList();

            foreach (var v in modelWithoutDuplicateInOrder)
            {
                Console.WriteLine(v.Name);
            }

            Console.ReadKey(true);

        }
    }
}

  

 

      Fig 4:

   In Fig 4  is showing the manufacturer list without any duplicate data in ascending order. 



     

Further Readings:

Responses
Author: Meganadha Reddy         Company URL: http://www.dotnetspark.com
Posted Date: October 08, 2010

Good article.
Thank you.

Regards,
Meganadha Reddy K.

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend