2012-01-10 25 views
1

我想知道是否有方法在LINQ to Entities中调用内置的sql函数?如'CAST','ISNULL'。我在互联网上搜索,我知道如何在LINQ to Entities中调用用户定义的函数,但我不知道如何调用内置函数。当然,一些内置函数可以用CLR方法代替,但如果你有办法直接调用它们,我会很感激。有没有办法在Linq中调用内置的sql函数实体

回答

1

SqlFunctions Class - 提供在LINQ to Entities查询中调用数据库中函数的公共语言运行时(CLR)方法。

如何使用

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); 
    } 
} 
+0

嗨@Pranay蛙,感谢您的回复!但我最初的帖子说我知道如何调用自定义函数,我真正想知道的是如何调用内置函数,如'CAST','ISNULL'。 – James 2012-08-08 12:04:59

+0

@James - 检查我的答案中给出的支持一些内置函数的SqlFunctions类链接... – 2012-08-08 12:51:40

相关问题