James
James

Reputation: 2610

Is there a way to call built-in sql function in Linq to Entities

I want to know if there is a way to call built-in sql functions in LINQ to Entities? Such as 'CAST', 'ISNULL'. I have searched on the internet and I know how to call user-defined functions in LINQ to Entities, but I don't know how to call built-in functions. Certainly, some built-in functions can be instead by CLR method, but I'll appreciate if you have a way to call them directly.

Upvotes: 1

Views: 966

Answers (1)

Pranay Rana
Pranay Rana

Reputation: 176886

SqlFunctions Class - Provides common language runtime (CLR) methods that call functions in the database in LINQ to Entities queries.

How to use

using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    // SqlFunctions.CharIndex is executed in the database.
    var contacts = from c in AWEntities.Contacts
                   where SqlFunctions.CharIndex("Si", c.LastName) == 1
                   select c;

    foreach (var contact in contacts)
    {
        Console.WriteLine(contact.LastName);
    }
}

Upvotes: 1

Related Questions