C# Keywords Tutorial Part 48: into

C# Keywords Tutorial Part 48: into

C# is a popular and modern object-oriented programming language that offers many features to developers for creating efficient and scalable applications. Among these features is the “into” keyword, which allows developers to transform a collection of items into a new collection with a different type.

This blog post will delve into the “into” keyword in C#, explaining its syntax and providing various examples to demonstrate how it can be used in different scenarios. Whether you’re working with LINQ, query expressions, or lambda expressions, understanding the “into” keyword will help you write cleaner and more efficient code.

Example 1: Transforming a List of Strings into a List of Integers

Consider a scenario where we have a list of strings containing numeric values, and we want to transform this list into a new list of integers. This can be achieved using the “into” keyword, as shown in the following code example:

List<string> stringList = new List<string>() { "1", "2", "3", "4", "5" };

List<int> intList = (from s in stringList
                     select Convert.ToInt32(s))
                    .Into<List<int>>();

In this example, we first define a list of strings called “stringList”, which contains numeric values represented as strings. We then use LINQ (Language-Integrated Query) to convert each string element to its corresponding integer value using the “select” clause.

The “Into” extension method is then used to transform the resulting collection of integers into a new list of integers, which is assigned to the “intList” variable. The resulting list contains the same elements as the original list, but with each element converted to its corresponding integer value.

Example 2: Grouping a List of Objects into a Dictionary

Another scenario where the “into” keyword can be useful is when we want to group a list of objects based on a specific property, and store the result in a dictionary. Consider the following code example:

class Employee
{
    public string Name { get; set; }
    public string Department { get; set; }
    public int Salary { get; set; }
}

List<Employee> employees = new List<Employee>()
{
    new Employee() { Name = "John", Department = "Sales", Salary = 50000 },
    new Employee() { Name = "Jane", Department = "Marketing", Salary = 60000 },
    new Employee() { Name = "Bob", Department = "Sales", Salary = 55000 },
    new Employee() { Name = "Mary", Department = "Marketing", Salary = 65000 }
};

Dictionary<string, List<Employee>> groupedEmployees = (from e in employees
                                                       group e by e.Department into grouped
                                                       select grouped)
                                                      .Into<Dictionary<string, List<Employee>>>();

In this example, we define a class called “Employee” with three properties: “Name”, “Department”, and “Salary”. We then create a list of “Employee” objects and initialize it with some data.

We use LINQ to group the employees by their department, resulting in a collection of IGrouping<string, Employee> objects. We then use the “Into” extension method to transform this collection into a dictionary, where the keys are the department names, and the values are lists of employees belonging to each department.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *