Method Syntax In LINQ

Method Syntax in LINQ is a powerful feature in C#, Used to filter a large collection of data. It uses the syntax of SQL to filter the objects in a collection.

Declare our Employee Class

Before diving into the code snippets, we must first create a class called “Employee”, and assign a list called “Employees” to a collection of “Employee” Objects.

class Program
{
static void Main(string[] args)
{
List<Employee> Employees = new List<Employee>() {
new Employee(5112, "John", 1000.34),
new Employee(5113, "Kennedy", 20045.10),
new Employee(5115, "John", 5000.23),
new Employee(5116, "Anna", 32400.43)
};
}
}
public class Employee
{
public int id { get; set; }
public string name { get; set; }
public double salary { get; set; }
        public Employee(int _id, string _name, double _salary)
{
id = _id;
name = _name;
salary = _salary;
}
}

Where

Using where clause to select a specific objects based on a given condition.

var empList = Employees.Where(emp => emp.name == "John");
           foreach(var emp in empList){
Console.WriteLine(emp.id);
// 5112
// 5115
}

This will filter the employees list and return the id of those who have the name of “John”. You might be wondering what’s “emp” and the syntax inside Where parameter.

Well, emp represents a reference to an employee Object, so that we can loop through all the values inside employees List and check if emp.name has the value of “John” or not.

The expression (emp => emp.name == “John”) is called “Lambda Expression” and this operator “=>” called Lambda Operator.

Select

Select will return true if the Object in the employees list matches the given condition, and false if not.

var empList = Employees.Select(emp => emp.salary > 1000.50);
            foreach(var emp in empList){
Console.WriteLine(emp);
// False, True, True, True.
}

GroupBy

Groups the elements of the list, according to the specified object property. Probably this is not the most suitable example to show GroupBy, but one thing you should notice is that, emp is a collection of groups, where each group has a collection of employee Objects.

So, i converted emp to a list, and fetched the first element, and then it’s salary. Again, maybe this is not the best example, but, you can do better with GroupBy.

var empList = Employees.GroupBy(emp => emp.name);
           foreach(var emp in empList){
Console.WriteLine(emp.ToList()[0].salary);
// 1000.34, 20045.10, 32400.43
}

Max

Get the max salary of all employees.

var empList = Employees.GroupBy(emp => emp.name);
           var empVar = Employees.Max(emp => emp.salary);
Console.WriteLine(empVar); // 32400.43
}

Sum

Get the sum of all salaries of all employees.

var empList = Employees.GroupBy(emp => emp.name);
           var empVar = Employees.Sum(emp => emp.salary);
Console.WriteLine(empVar); // 58446.1
}

OrderBy

Get the list of all employees ordered by their ids, You can either choose Descending or Ascending(Default).

var empList = Employees.GroupBy(emp => emp.name);
           // var empList = Employees.OrderBy(emp => emp.id);
var empList = Employees.OrderByDescending(emp => emp.id);
foreach(var emp in empList){
Console.WriteLine(emp.name);
// Anna, John, Kennedy, John.
}
}

Wrapping Up

Now, you can see how Method Syntax in LINQ is very powerful and makes collection objects are much easier, which is something you will do often. Here is a list of all Operations you can perform.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.