Partager via


LINQ Farm: Querying a Collection of Integers

This simple example shows how to use LINQ to Objects to query a collection of Integers. Some readers might find it odd to speak of querying an array of numbers, but collections contain data just as a database or an XML file contains data. A key feature of LINQ is that it allows you to use a similar syntax to query databases, XML, or a data structure in your program such as a collection of integers.

The C# 3.0 feature called a Collection Initializer provides a handy shorthand way to populate a collection. Here is how to automatically initialize a list of numbers:

 List<int> list = new List<int>() { 1, 2, 3 };

Listing One shows a complete program demonstrating how to write a LINQ query against this collection. The query selects all the numbers in the collection that are smaller than 3 and prints them to the screen.

Listing One: A simple numeric query that returns the values 1 and 2.

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

namespace NumericQuery
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 1, 2, 3 };

            var query = from number in list
                        where number < 3
                        select number;

            foreach (var number in query)
            {
                Console.WriteLine(number);
            }
        }
    }
}

The centerpiece of the code shown in Listing 1 is the LINQ query itself, which is called a query expression:

 var query = from number in list
            where number < 3
            select number;

On the left of the equals operator you see the words “var query.” The new keyword var tells the compiler to use type inference to infer the type of the identifier query from the expression on the right of the operator. This new keyword eliminates the need to guess the type of the data returned from a LINQ query, or from most other expressions. Just type the keyword var and let the compiler do the rest of the work for you.

On the right side of the equals operator you see the query itself:

   from number in list
  where number < 3
  select number;

All query expressions begin with the keyword from, and they generally end with line that begins with the keyword select. This particular query expression asks the compiler to iterate over the numbers in the list, returning only those that are smaller than 3. A foreach loop iterates over the results of the query. As mentioned early, this loop prints out the numbers 1 and 2.

This simple program introduced three key concepts:

  1. Collection Initializers
  2. Type Inference
  3. Query Expressions

kick it on DotNetKicks.com

Comments

  • Anonymous
    November 04, 2007
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    November 04, 2007
    Welcome to the thirty-fifth edition of Community Convergence. This week we have an interview with C#

  • Anonymous
    July 09, 2008
    Generic zoloft. Zoloft. Zoloft indications.

  • Anonymous
    July 16, 2008
    Work from home no fees. Work at home.

  • Anonymous
    May 22, 2009
    I have the array of integers: d[]. for example: 4 1 8 0 3 2 6 7 0 4 9 1 1 5 0 6 1 3 5 2 0 and so on. At compile time I do not know how many columns has array d. And I do not know at compile time which columns to use to orderby. For example need to sort by: d[5], d[2], d[0], d[3]. But at run time I know order of the columns to sort. For example column indexes: 5, 2, 0, 3 which mean d[5], d[2], d[0], d[3] columns. How can I orderby this indexes of columns?