CLR Method Calls and Function Mapping

The Entity Framework provides a set of canonical functions that implement functionality that is common across many database systems, such as string manipulation and mathematical functions. This enables developers to target a broad range of database systems. When called from a querying technology, such as LINQ to Entities, these canonical functions are translated to the correct corresponding store function for the provider being used. This allows function invocations to be expressed in a common form across data sources, providing a consistent query experience across data sources. The bitwise AND, OR, NOT, and XOR operators are also mapped to canonical functions when the operand is a numeric type. For Boolean operands, the bitwise AND, OR, NOT, and XOR operators compute the logical AND, OR, NOT, and XOR operations of their operands. For more information, see Canonical Functions (Entity SQL).

For LINQ scenarios, queries against the Entity Framework involve mapping certain CLR methods to methods on the underlying data source through canonical functions. Any method calls in a LINQ to Entities query that are not explicitly mapped to a canonical function will result in a runtime NotSupportedException exception being thrown. For a list of CLR methods that are mapped to canonical functions, see CLR Method to Canonical Function Mapping.

The following example queries for addresses that are located on Algiers Drive. The Contains method call in the query is mapped to the IndexOf canonical function.

Using AWEntities As New AdventureWorksEntities()
    Dim addresses As ObjectQuery(Of Address) = AWEntities.Address

    Dim query = _
    From address In addresses _
    Where address.AddressLine1.Contains("Algiers Dr.") _
    Select address

    For Each algiersAddress As Address In query
        Console.WriteLine("Address 1: " + algiersAddress.AddressLine1)
    Next

End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<Address> addresses = AWEntities.Address;

    IQueryable<Address> query = from address in addresses
                                where address.AddressLine1.Contains("Algiers Dr.")
                                select address;

    // Addresses on Algiers Dr.
    foreach (Address algiersAddress in query)
    {
        Console.WriteLine("Address 1: " + algiersAddress.AddressLine1);
    }
}

See Also

Concepts

Canonical Functions (Entity SQL)

Other Resources

Querying with LINQ to Entities