在 LINQ 中使用外部函数

发布于 2024-10-28 05:22:05 字数 222 浏览 2 评论 0原文

是否可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瑶 2024-11-04 05:22:05

这取决于所选的提供商。使用基于 IEnumerable 查询的普通 LINQ(又名 LINQ to Objects),您可以做到这一点。但是,对于大多数基于表达式树的 LINQ(例如 LINQ to SQL、LINQ to Entities、NHibernate、LLBLGen 等),您不能这样做。这些提供程序将表达式树转换为其他语言,例如 SQL、AD、SharePoint 等。他们只是不知道如何处理您的自定义方法。

您可以通过多种方式解决这个问题。例如,尝试在查询中内联编写方法的代码。这样,提供者就知道如何处理它。另一种选择是让该方法在表达式树之外调用。例如(对不起我的 C#):

var q = (from a in context select a).AsEnumerable();

// q is an IEnumerable and the z method will be called by .NET instead
// of being translated to SQL.
var q2 = (from a in q select new { z = z("MYNAME") };

It depends upon the choosen provider. With normal LINQ over IEnumerable 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#):

var q = (from a in context select a).AsEnumerable();

// q is an IEnumerable and the z method will be called by .NET instead
// of being translated to SQL.
var q2 = (from a in q select new { z = z("MYNAME") };
您的好友蓝忘机已上羡 2024-11-04 05:22:05

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文