Dimensions.All(y => x.DimensionSet.Entries.Any(dim => (d.Dimensi-6ren">
gpt4 book ai didi

c# - 使用 MethodCallExpression 调用表达式 "Any"

转载 作者:行者123 更新时间:2023-11-30 20:28:32 28 4
gpt4 key购买 nike

我正在尝试实现以下代码:

db.Invoices.Where(x => Dimensions.All(y => x.DimensionSet.Entries.Any(dim => (d.DimensionValue.DimensionCode + "_" + d.DimensionValue.Value) == y))

我尝试过的:

/* Dimensions Logic
* Copy the following logic:
* {&& Dimensions.All(y => x.DimensionSet.Entries.Any(d => (d.DimensionValue.DimensionCode + "_" + d.DimensionValue.Value) == y))}
*/

/* Get expression of the nested property Func<string, bool> to imitate the second argument of `Dimensions.All` */
Expression entriesExpression = Expression.Property(body, "Entries");

/* Get expression of the current Dimensions property */
Expression dimensionsExpression = Expression.Constant(Dimensions);

Type dimensionsAllType = typeof(Func<,>).MakeGenericType(typeof(string), typeof(bool));
Type innerAnyType = typeof(Func<,>).MakeGenericType(typeof(DimensionSetEntry), typeof(bool));

/* Get the `All` method that may be used in LINQ2SQL
* Making a generic method will guarantee that the given method
* will match with the needed parameters.
* Like it was a "real" linq call.
*/
MethodInfo methodAll =
typeof(Enumerable)
.GetMethods()
.FirstOrDefault(x => x.Name == "All")
.MakeGenericMethod(dimensionsAllType);

MethodInfo methodAny =
typeof(Enumerable)
.GetMethods()
.FirstOrDefault(x => x.Name == "Any")
.MakeGenericMethod(innerAnyType);

MethodCallExpression call_Any = Expression.Call(
null,
methodAny,
entriesExpression,
Expression.Lambda(Expression.Constant(true), Expression.Parameter(typeof(DimensionSetEntry), "y"))
);

MethodCallExpression call_All = Expression.Call(
null,
methodAll,
dimensionsExpression,
call_Any
);

现在,我只是在为表达式调用而苦苦挣扎。

MethodCallExpression call_Any = Expression.Call(
null,
methodAny,
entriesExpression,
Expression.Lambda(Expression.Constant(true), Expression.Parameter(typeof(DimensionSetEntry), "y"))
);

我在这里尝试调用 Any Enumerable的方法| .表达式 entriesExpression代表x.DimensionSet.Entries (类型:ICollection<DimensionSetEntry>)

下一个参数表示常量 x => True目的只是暂时测试调用,但在这里我需要插入 (d.DimensionValue.DimensionCode + "_" + d.DimensionValue.Value) == y .

但是,调用这个的时候,会出现如下错误:

Incorrect number of arguments supplied for call to method 'Boolean Any[Func2](System.Collections.Generic.IEnumerable1[System.Func`2[EmployeePortal.Models.DimensionSetEntry,System.Boolean]])'

最佳答案

Any()有两个重载;一个只需要 IEnumerable<T>还有一个也接受 lambda。

你的 methodAny变量可能包含第一个。您需要更改该变量以找到具有两个参数的重载。

关于c# - 使用 MethodCallExpression 调用表达式 "Any",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47185505/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com