Advertisement
Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Oct 27, 2022

LINQ — How to avoid nested loops in C#

Автор:
Sebastian Streng
Источник:
Просмотров:
2143

Writing code can be very exciting but it also can be very frustrating if your code is based on nested loops. Iterations are still one the most important parts of coding. So how can we avoid using ugly nested loops in our code?

LINQ — Language Integrated Query

Microsoft language developers provided a way to express queries directly in their languages. LINQ extends the language by the addition of query expressions, which are similar to SQL-queries.

LINQ syntax is typically less efficient than a foreach loop. It’s good to be aware of any performance tradeoff that might occur when you use LINQ. On the other hand, its the most efficiant way to improve the readability of your code.

Benefits of using LINQ

  • Developers can access new Technologies without knowing much about them.
  • LINQ offers an object-based, language-integrated way to query over data no matter where that data came from. So through LINQ we can query database, XML as well as collections.
  • create complete Applications with less code.
  • develop Application in less time and with fewer errors.
  • combine datasources without resorting to odd programming tricks.
  • Get new Developers working faster.
  • Compile time syntax checking.
  • allows you to query collections like arrays, enumerable classes etc in the native language of your application, like VB or C# in much the same way as you would query a database using SQL.

“Perhaps the most important aspect of LINQ which I’d love other API designers to take on board is that of how complicated queries are constructed from lots of little building blocks. What makes it particularly elegant is that the result of applying each building block is unchanged by anything else you do to it afterwards.

LINQ doesn’t enforce immutability of course — you can start off with a mutable list and change its content at any time, for example, or change the properties of one of the objects referenced within it, or pass in a delegate with side-effects — but LINQ itself won’t introduce side-effects.” (Skeet, 03.2018)

Methods:

Loops using index

One of the most common mistakes using LINQ is that developers think its not possible to replace a for-loop that calls methods based on its index. Here are some examples that shows how to do it.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

// bad code:
int c = 1;
for (int i = 0; i < numbers.Length; ++i)
{
    c *= numbers[i];
}

// good code:
int c2 = numbers.Aggregate(1, (current, t) => current*t);

//Combine Foreach-loop with index:
List<string> bestColleagues = new List<string>
{
    "Tobi", "Jan", "Sebastian"
};

foreach (var colleague in bestColleagues.Select((name, index) => (name, index)))
{
    Console.WriteLine($"{colleague.name} is number {colleague.index +1} on this list." );
}

// Get the index of the number 8.
var indexes = numbers
    .Select((x, i) => i)
    .Where((i) => numbers[i] == 8);

Nested loops using Linq

The first example takes about 17 lines. The second one just 3.

Lets imagine we need to navigate through an Excelworksheet using EPPlus. Using loops to access each individual cell can always be very expensive. Nested for-loops are often used here, which take up a lot of space and make reading complicated. Lets see how we can avoid nested loops:

private static void DoSomethingInExcel(ExcelWorksheet worksheet)
{
    // Now we need to navigate to a X/Y-System in Excel.
    // bad Code:
    var startRow = 1;
    var endrow = worksheet.Dimension.Rows;
    for (int row = startRow; row <= endrow; row++)
    {
        var startColumn = 1;
        var endColumn = worksheet.Dimension.Columns;
        for (int col = startColumn; col < endColumn; col++)
        {
            if (worksheet.Cells[row, col].Style.Font.Bold == true)
            {
                var cellContent = Convert
                    .ToInt32(worksheet.Cells[row, col].Value
                    .ToString());

                DoSomething(cellContent);
            }
        }
    }
    //--------------------------------------------------------------------------------

    // good Code:

     worksheet.Cells[worksheet.Dimension.Address]
        .Where(c => c.Style.Font.Bold)
        .Select(c => DoSomething(Convert.ToInt32(c)));
}

Conclusion

all in all, the advantages of Linq outweigh the disadvantages.
The loss of speed versus clean readability, faster writing, and application of SQL-based language suggests that using Linq-expressions offers powerful benefits to the developer.

Further Examples:

static void Main(string[] args)
{
    int[] randomNumbers = { 127, 264, 33657, 43, 535, 6, 83652, 73, 652, 3043, 236, 896 };

    // Filter array

    // BadPractice:
    List<int> numberslist = new ();
    foreach(int number in randomNumbers)
    {
        if (number.ToString().Contains("3")) {
            numberslist.Add (number);
        }
    }

    // Good Practice:
    var numberList2 = randomNumbers
        .Where(n => n.ToString().Contains("3"));

    // Now we can use Linq-expressions:

    // Get Every number until 999,
    // Call Method in Linq-expression,
    // Order every Element by Descending
    // and return first element of the list
    int firstElement = randomNumbers
         .Where(n => n.ToString().Length < 4)
         .Select(n => DoSomething(n))
         .OrderByDescending(n => n)
         .FirstOrDefault();

    // Using SelectMany() for Multi-Dimensional IEnumerable:
    // Get every Vehicle that contains a "a"
    var VehiclesThatContainsA = GetDoubleArray()
        .SelectMany(v => v)
        .Where(v => v.Contains("a"));
}

public static int DoSomething(int randomNumber)
{
    return randomNumber + 10;
}

public static IEnumerable<IEnumerable<string>> GetDoubleArray()
{
    List<string[]> vehicles = new ();
    string[] cars = {"Audi", "BMW", "Ford", "Porsche"};
    string[] motorcycles = { "Honda", "Suzuki", "BMW", "Yamaha"};
    string[] trucks = { "Mercedes-Actros", "Scania", "Iveco" };

    vehicles.Add(cars);
    vehicles.Add(motorcycles);
    vehicles.Add(trucks);

    return vehicles;
}
Похожее
Jul 7, 2021
Author: Changhui Xu
C# has a feature, String Interpolation, to format strings in a flexible and readable way. The following example demonstrates the way how we usually output a string when we have data beforehand then pass data to the template string.var name...
Jan 28, 2023
Author: Rokas
Prerequisites: VSCode and .Net 7 installed. Don't need to copy the code, GitHub link provided in the end.Open up a terminal in VSCode or any IDE of your choice, run:dotnet new console2 files will appear, default Program.cs and .csproj named...
Feb 2
Author: Achref Hannachi
IntroductionLINQ (Language Integrated Query) is a powerful feature in C# that allows developers to perform complex queries on collections and databases using a syntax that is both expressive and readable. However, writing LINQ queries efficiently is essential to ensure that...
Dec 1, 2023
Author: Rico Fritzsche
The flight monitor case study: Applying Vertical Slices in a real-world scenarioIn my last articles I talked about Vertical Slice Architecture and how to organize code in a non-technical way. I showed some simple examples of how to do this...
Написать сообщение
Почта
Имя
*Сообщение


© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban