向LINQ中的实体添加函数
假设我有一个使用northwind数据库的实体向LINQ中的实体添加函数,linq,vb.net-2010,Linq,Vb.net 2010,假设我有一个使用northwind数据库的实体客户 如果我在部分类customer下创建了一个函数getcustomerbyd。我可以使用Linq查询执行此函数,例如 Dim q = From t in db.Customers where t.GetCustomerByID(my_parameter) Select T 如果我在分部类dx\u northwindDataContext下创建函数。我可以通过以下方式执行此功能: Dim result = db.GetCustomerByID(m
客户
如果我在部分类customer
下创建了一个函数getcustomerbyd
。我可以使用Linq
查询执行此函数,例如
Dim q = From t in db.Customers where t.GetCustomerByID(my_parameter) Select T
如果我在分部类dx\u northwindDataContext
下创建函数。我可以通过以下方式执行此功能:
Dim result = db.GetCustomerByID(my_parameter)
因为我正在为我的数据库创建数据访问层,并且没有将我的所有函数公开给db
变量的所有实体。我在寻找一种调用函数的方法,如:
Dim result = db.Customers.GetCustomerByID(my_parameter)
是否有其他方法可以这样做?是的,您可以为db创建。客户
喜欢:
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
导入System.Runtime.CompilerServices
IQueryableExtensions模块
公共函数GetCustomerByID(ByVal源为IQueryable(属于客户),ByVal id为Int32)为IQueryable(属于客户)
返回source.Where(函数(f)f.Id==Id)
端接头
端模块