我编写了一些代码来制作动态表达式来过滤我的分页。我正在尝试制作 EF Core 内置函数的动态表达式以进行搜索 ( EF.Functions.Like
)。
我尝试过像bottom这样的方法,但它是一个扩展方法,调用该方法时不使用第一个参数。我不知道如何遵循==> Ef => Function => Like的方式。
该方法应该像这样使用=>Ef.Functions.Like("Property to search", "%Some Pattern")
var likeMethod = typeof(DbFunctionsExtensions)
.GetMethods()
.Where(p => p.Name == "Like")
.First();
string pattern = $"%{finalConstant}%";
ConstantExpression likeConstant = Expression.Constant(pattern,typeof(string));
// the member expression is the property expression for example p.Name
var likeMethodCall = Expression.Call(method: likeMethod, arguments: new[] { memberExpression, likeConstant });
var searchLambda = Expression.Lambda<Func<T, bool>>(likeMethodCall, parameter);
query = query.Where(searchLambda);
Run Code Online (Sandbox Code Playgroud)
但它抛出异常说
为调用方法“Boolean Like(Microsoft.EntityFrameworkCore.DbFunctions, System.String, System.String)”提供的参数数量不正确\r\n参数名称: 方法
c# .net-core asp.net-core-webapi asp.net-core-2.0 ef-core-2.0
我有一个带有 .net core 2.1 的 API,并在操作中使用取消令牌。当我使用 iis 运行项目并在浏览器中发出一个简单的请求时,在我取消它后,它无法按预期工作并运行所有传递给它们的取消令牌的方法。但是当我使用 kestrel 服务器运行项目时,取消请求会导致服务器按预期工作。这是为什么?
[HttpGet("get")]
public async Task<IActionResult> GetSome(CancellationToken ct)
{
_logger.LogInformation("The slow Request Start");
try
{
await Task.Delay(10_000, ct);
_logger.LogCritical("Successful");
}
catch (Exception)
{
_logger.LogInformation("Task Cancelled");
}
var message = "The request finished";
_logger.LogInformation(message);
return NoContent();
}
Run Code Online (Sandbox Code Playgroud)