Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向LINQ中的实体添加函数_Linq_Vb.net 2010 - Fatal编程技术网

向LINQ中的实体添加函数

向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

假设我有一个使用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(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)
端接头
端模块