2013-05-19 27 views
1

假設我有一個使用northwind數據庫的實體customer將函數添加到LINQ中的實體

如果我在partial class customer下創建了功能GetCustomerByID。我可以用Linq查詢如

Dim q = From t in db.Customers where t.GetCustomerByID(my_parameter) Select T 

如果我partial class dx_northwindDataContext下創建的函數執行此功能。我可以通過執行此功能:

Dim result = db.GetCustomerByID(my_parameter) 

自從我創建數據訪問層爲我的數據庫,不暴露我的所有功能,我所有的實體到db變量。我正在尋找一種叫我的功能的方式,如:

Dim result = db.Customers.GetCustomerByID(my_parameter) 

有沒有那樣做?

回答

2

是的,你可以爲db.Customers如創建an extension

Imports System.Runtime.CompilerServices 

    Module IQueryableExtensions 

     <Extension()> 
     Public Function GetCustomerByID(ByVal source As IQueryable(Of Customer), ByVal id As Int32) As IQueryable(Of Customer) 
      return source.Where(Function(f)f.Id == id) 
     End Sub 

    End Module 
+0

它就像一個魅力..謝謝。 –