2012-01-10 51 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

相關問題