O'Reilly logo

Developer’s Guide to Collections in Microsoft® .NET by Calvin “Lee” Janes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. Introduction to LINQ

After completing this chapter, you will be able to

  • Understand what LINQ is.

  • Write LINQ queries against collection classes.

What Is LINQ?

In the previous chapters, you saw how to create and interact with collections using the foreach statement and Microsoft .NET interfaces. In this chapter, you will discover a different approach: how to query your collections with the Language Integrated Query (LINQ). With LINQ, you can query data by using native language syntax.

Here’s an example that does not use LINQ.

C#

Random rnd = new Random(); List<int> randomNumbers = new List<int>(); for (int i = 0; i < 10; i++) { randomNumbers.Add(rnd.Next(100)); } List<int> sortedNumbers = new List<int>(randomNumbers); sortedNumbers.Sort(); sortedNumbers.Reverse(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required