Vb.net VB中通过API实现LINQ表达式到表达式树
我有一个相对简单的LINQ表达式,需要将其转换为VB表达式树语法。对于熟悉LINQ表达式树的人来说,这可能是一项简单的任务,但我对LINQ表达式树的领域还不熟悉 在我的示例中,您会看到一个“newint16(){}”数组。该值必须在运行时使用来自另一个代码元素的值数组进行参数化 我的问题是:Vb.net VB中通过API实现LINQ表达式到表达式树,vb.net,linq,api,linq-to-sql,lambda,Vb.net,Linq,Api,Linq To Sql,Lambda,我有一个相对简单的LINQ表达式,需要将其转换为VB表达式树语法。对于熟悉LINQ表达式树的人来说,这可能是一项简单的任务,但我对LINQ表达式树的领域还不熟悉 在我的示例中,您会看到一个“newint16(){}”数组。该值必须在运行时使用来自另一个代码元素的值数组进行参数化 我的问题是: from i in tblInstitutions let ChildHasCategory = i.tblInstCtgyHistories.Where(Function(CtgyHist) CtgyHi
from i in tblInstitutions
let ChildHasCategory = i.tblInstCtgyHistories.Where(Function(CtgyHist) CtgyHist.EndDate is Nothing AND ( (New Int16() {32,35,38,34}).Contains(CtgyHist.InstCtgyCodeFK)))
where ChildHasCategory.Any()
select i
也可以表示为:
tblInstitutions
.Select (i => new {
i = i,
ChildHasCategory = (IEnumerable<tblInstCtgyHistory>)(i.tblInstCtgyHistories)
.Where (
CtgyHist =>
((CtgyHist.EndDate == null) &
(IEnumerable<Int16>)(new Int16[] { 32, 35, 38, 34 } ).Contains (CtgyHist.InstCtgyCodeFK)
)
)
}
)
.Where ($VB$It => $VB$It.ChildHasCategory.Any ())
.Select ($VB$It => $VB$It.i)
我不确定你真的需要担心这里的表达式树。首先,我们应该能够表达您的疑问如下:
Dim targetCodes = new Int16() {32, 35, 38, 34 } ' This could be data driven as well
return from i in tblInstitutions
where i.tblInstCtgyHistories.Any(Function(ctgyHist) ctgyHist.EndDate is Nothing AndAlso
targetCodes.Contains(ctgyHist.InstCtgyCodeFK))
select i
既然如此,在什么情况下需要自定义表达式树?查询可以简化为:tblInstitutions where中的from i(i.tblInstCtgyHistories.where(Function(CtgyHist)CtgyHist.EndDate为Nothing和((New Int16(){32,35,38,34}).Contains(CtgyHist.InstCtgyCodeFK)))。Any()选择iCorrect,查询可以这样表示。但是,我需要将其转换为一种格式,该格式可以作为动态数据应用程序自定义过滤器的GetQueryable函数的一部分使用。它使用表达式调用和API语法方法,因为过滤器需要与动态数据应用程序的所有其他选定过滤器混合。我似乎无法找到Expression.Call和Expression.Parameter方法的正确组合来完成我所需要的。我相信当它完成时,可能只有4或5行代码,但这是一个巨大的障碍。只要您仍在查询中工作,您就应该能够使用并扩展该表达式。
Dim targetCodes = new Int16() {32, 35, 38, 34 } ' This could be data driven as well
return from i in tblInstitutions
where i.tblInstCtgyHistories.Any(Function(ctgyHist) ctgyHist.EndDate is Nothing AndAlso
targetCodes.Contains(ctgyHist.InstCtgyCodeFK))
select i