linq - 使用 ef 核心中的 OR 条件基于所有可选参数的动态查询
问题描述
我不确定我是不是找错了树,但我想创建一个函数来根据所有可选参数检查帐户是否存在,这样它就可以用来根据你想要检查的任何内容来提取数据.
基本上,查询应该是:其中 loginName = p1 或 loginName = p2 或 loginName = p3 但参数都是可选的,但至少会提供一个。
这是我到目前为止所尝试的:
public async Task<bool> CheckAccountExistsAsync(string loginName = "", string authenticatorId = "", string eId = "")
{
if (string.IsNullOrWhiteSpace(loginName) && string.IsNullOrWhiteSpace(authenticatorId) && string.IsNullOrWhiteSpace(eId))
throw new InvalidOperationException("You must pass at least one parameter");
return await _context.Accounts.AnyAsync(a =>
(string.IsNullOrWhiteSpace(loginName) || a.LoginName == loginName)
|| (string.IsNullOrWhiteSpace(authenticatorId) || a.AuthenticatorId == authenticatorId)
|| (string.IsNullOrWhiteSpace(eId) || a.EmployeeId == eId));
}
这种方法的问题是,如果我只传递登录名,那么查询如下,条件完全省略:
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Accounts] AS [a]) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END
我确定我错过了一些东西,有没有更好的方法?
解决方案
您使用的适用于可选and
表达式,例如
return await _context.Accounts.AnyAsync(a =>
(string.IsNullOrWhiteSpace(loginName) || a.LoginName == loginName)
&& (string.IsNullOrWhiteSpace(authenticatorId) || a.AuthenticatorId == authenticatorId)
&& (string.IsNullOrWhiteSpace(eId) || a.EmployeeId == eId));
对于可选or
的,您必须使用可选的and
子条件并添加对所有可选参数缺失的附加检查,例如
return await _context.Accounts.AnyAsync(a =>
(string.IsNullOrWhiteSpace(loginName)
&& string.IsNullOrWhiteSpace(authenticatorId)
&& string.IsNullOrWhiteSpace(eId))
|| (!string.IsNullOrWhiteSpace(loginName) && a.LoginName == loginName)
|| (!string.IsNullOrWhiteSpace(authenticatorId) && a.AuthenticatorId == authenticatorId)
|| (!string.IsNullOrWhiteSpace(eId) && a.EmployeeId == eId));
推荐阅读
- reactjs - Formik is missing a value specified otherwise in initialValues
- swift - Exclude platform-specific resources in Package.swift
- dataflow - Azure Synapse Workspace = data flow debug error
- api - create a charge with Stripe and Delphi
- python - 如何在多个功能中最佳使用 numba?
- c++ - 数字文字到无符号类型的类型推导
- javascript - Ngrx "Observable" vs Ng Observable
- amazon-web-services - 使用 Buildspec.yml 将构建工件复制到 s3 存储桶
- firebase - 如何从具有唯一文档 ID 的 Firebase 中获取数据并将其存储到下面代码中提到的变量中以在 _profilename 中使用?
- mysql - 具有 Cloud MySQL 授权的 Google Cloud Function 失败([Errno 111] 连接被拒绝)