在 LINQ 中使用外部函数
是否可以在 linq select 结果中使用外部函数? 以及如何将参数传递给它?
sub x
Dim q = From a In contex Select New With {.z=z("MYNAME")}
end sub
function z(name as string)
return( name & "Something...")
end function
is it possible to use external function in linq select resault ?
and how to pass param to it?
sub x
Dim q = From a In contex Select New With {.z=z("MYNAME")}
end sub
function z(name as string)
return( name & "Something...")
end function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于所选的提供商。使用基于
IEnumerable
查询的普通
LINQ(又名 LINQ to Objects),您可以做到这一点。但是,对于大多数基于表达式树的 LINQ(例如 LINQ to SQL、LINQ to Entities、NHibernate、LLBLGen 等),您不能这样做。这些提供程序将表达式树转换为其他语言,例如 SQL、AD、SharePoint 等。他们只是不知道如何处理您的自定义方法。您可以通过多种方式解决这个问题。例如,尝试在查询中内联编写方法的代码。这样,提供者就知道如何处理它。另一种选择是让该方法在表达式树之外调用。例如(对不起我的 C#):
It depends upon the choosen provider. With
normal
LINQ overIEnumerable
queries (a.k.a. LINQ to Objects) you can do. However, with most LINQ over Expression trees (such as LINQ to SQL, LINQ to Entities, NHibernate, LLBLGen, etc, etc) you can't. Those providers convert an expression tree to some other language, such as SQL, AD, SharePoint, etc, etc. They just don't know what to do with your custom method.You can solve this in several ways. For instance, try to write the code of the method inline in the query. This way the provider knows what to do with it. Another option is to let the method call outside of the expression tree. For instance (sorry my C#):
StackOverflow 已在此处对此问题进行了解答和解释。请看一下。
您可以在此处找到您想要执行的操作的完整说明。
That has been answered and explained here at StackOverflow. Please have a look.
And the complete explanation of what you are trying to do can be found here.