Home

LINQ Keywords: from, in, and select

   

Introduction

To perform a basic query in LINQ, you can use the following formula:

var SubListName = from ValueHolder in List select ValueHolder;

The var keyword, the assignment operator "=", the from keyword, the in keyword, the select keyword, and the semicolon are required.

The SubListName is a name of a new variable that will hold the list of values produced by this operation.

The ValueHolder is the name of a variable that will be used to identify each resulting member of this operation. This variable will be equivalent to getting each member of the list and that responds to a condition.

The List factor represents the name of the variable that you would have created already. The List can be an array. Here is an example:

using System;
using System.Linq;

public class Exercise
{
    public static int Main()
    {
        var numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 };

        var number = from n in numbers select n;

        foreach (var member in number)
            Console.WriteLine("Member: {0}", member.ToString());
        return 0;
    }
}

This would produce:

Numbers

To make the code easier to read, you can spread the select statement to various lines. Here is an example:

var number = from n
	     in numbers
	     select n;

Home Copyright © 2008-2016, FunctionX, Inc.