Compartir a través de


select ("M" Reference)

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

The select keyword is used to select elements from a collection.

Syntax

Collection select Expression

Discussion

The select keyword takes a collection on the left and an arbitrary expression on the right. It introduces the keyword identifier value that ranges over each element in the collection. The select keyword maps the expression over each element in the collection and returns the result. The following expression:

Collection select Expression

is exactly equivalent to:

from value in Collection
select Expression

Examples

Selecting a Single Field

The following example shows how to select a single field from each element in a collection. The three functions ProductNames1(), ProductNames2(), and ProductNames3() demonstrate different syntax to achieve the same functionality. The ProductNames functions iterate across the Products collection and return a collection that contains the ProductName fields from each element.

module Sample1 
{
    type Product 
    {
       Id : Integer32 = AutoNumber();
       ProductName : Text;
       Category : Text;
       Supplier : Text;
       QuantityPerUnit : Integer32;
       UnitPrice : Integer32;
    } where identity Id;

    Products : Product*;    

    ProductNames1() 
    { 
        from p in Products
        select p.ProductName
    }
    ProductNames2() 
    { 
        Products select value.ProductName
    }
    ProductNames3() 
    {
       Products.ProductName
    }
}

Selecting Multiple Fields

The following example shows how to select multiple fields from each element in a collection. The three functions ProductInfo1() and ProductInfo2() demonstrate different syntax to achieve the same functionality. The ProductInfo functions iterate across the Products collection and return a collection of entities. Each entity contains a ProductName, Category, and UnitPrice from an element within Products.

module Sample2 
{
    type Product 
    {
       Id : Integer32 = AutoNumber();
       ProductName : Text;
       Category : Text;
       Supplier : Text;
       QuantityPerUnit : Integer32;
       UnitPrice : Integer32;
    } where identity Id;

    Products : Product*;

    ProductInfo1() 
    { 
        from p in Products
        select 
        { 
            ProductName = p.ProductName, 
            Category = p.Category,
            Price = p.UnitPrice
        }
    }
    ProductInfo2() 
    { 
        Products select 
        { 
            ProductName = value.ProductName, 
            Category = value.Category,
            Price = value.UnitPrice
        }
    }
}

Selecting Entities that meet Specified Criteria

The following example shows how to select entities that meet specified criteria. The three functions IndustrialProducts1(), IndustrialProducts2(), and IndustrialProducts3() demonstrate different syntax to achieve the same functionality. The IndustrialProducts functions iterate across the Products collection and return a collection of all Product entities whose Category field contains "Industrial”.

module Sample3 
{
    type Product 
    {
       Id : Integer32 = AutoNumber();
       ProductName : Text;
       Category : Text;
       Supplier : Text;
       QuantityPerUnit : Integer32;
       UnitPrice : Integer32;
    } where identity Id;

    Products : Product*;
   
    IndustrialProducts1() 
    { 
        from c in Products
        where c.Category == "Industrial"
        select c
    }

    IndustrialProducts2() 
    { 
        Products where value.Category == "Industrial"
    }
    
    IndustrialProducts3() 
    {
        Products.Category("Industrial")
    }
}

Modifying Selected Values

The following example shows one way to modify selected values. The two functions NumsPlusOne1() and NumsPlusOne2() demonstrate different syntax to achieve the same functionality. The NumsPlusOne functions returns a set of numbers that are all one greater than the values found within the Numbers collection. When either function is called, the collection {6, 5, 2, 4, 10, 9, 7, 8, 3, 1} is returned.

module Sample4
{
    Numbers : Integer32* { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }
    
    NumsPlusOne1() 
    {
        from n in Numbers
        select n + 1
    }
    NumsPlusOne2() 
    {
        Numbers select value + 1
    }
}