gpt4 book ai didi

linq - 在 LINQ to EF 查询中构建动态 where 子句

转载 作者:行者123 更新时间:2023-12-03 08:29:31 25 4
gpt4 key购买 nike

我试图在通过 EF 引用表的 LINQ 查询中动态构建 where 子句,但出现以下错误:

'ClaimantLastName' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near member access expression, line 6, column 2.

这是我正在尝试的:

string whereClause = string.Format("ClaimantLastName = '{0}' and ClaimantSSN = '{1}'", lastName, ssn);

我也试过不加单引号也没用。

这是实际的查询:

return db.Claims.Where(whereClause).Select(
u => new AdvancedSearchResult
{
ClaimNumber = u.ClaimNumber,
.
.
.

我想做的事情可行吗?看起来真的很基本。我哪里错了?

更新:这是声明实体。

public static Claim CreateClaim(global::System.Int32 id, global::System.String claimantFirstName, global::System.String claimantLastName, global::System.String claimantSSN, global::System.DateTime dateOfInjury, global::System.String claimNumber, global::System.String claimantMiddleName, global::System.String claimantAddress1, global::System.String claimantAddress2, global::System.String claimantCity, global::System.String claimantState, global::System.String claimantZip, global::System.DateTime claimantDateOfBirth, global::System.String compensability, global::System.Boolean injuryType, global::System.String jurisdictionState, global::System.String status, global::System.String condition, global::System.String managingBranch, global::System.String bodyPart, global::System.String acceptedBodyPart, global::System.Boolean pGCase, global::System.String employersDefenseAttorney, global::System.String accidentDescription, global::System.String claimExaminerFirstName, global::System.String claimExaminerLastName, global::System.String claimExaminerEmail, global::System.String claimantAttorney, global::System.String workerId, global::System.String workerType)
{
Claim claim = new Claim();
claim.Id = id;
claim.ClaimantFirstName = claimantFirstName;
claim.ClaimantLastName = claimantLastName;
claim.ClaimantSSN = claimantSSN;
claim.DateOfInjury = dateOfInjury;
claim.ClaimNumber = claimNumber;
claim.ClaimantMiddleName = claimantMiddleName;
claim.ClaimantAddress1 = claimantAddress1;
claim.ClaimantAddress2 = claimantAddress2;
claim.ClaimantCity = claimantCity;
claim.ClaimantState = claimantState;
claim.ClaimantZip = claimantZip;
claim.ClaimantDateOfBirth = claimantDateOfBirth;
claim.Compensability = compensability;
claim.InjuryType = injuryType;
claim.JurisdictionState = jurisdictionState;
claim.Status = status;
claim.Condition = condition;
claim.ManagingBranch = managingBranch;
claim.BodyPart = bodyPart;
claim.AcceptedBodyPart = acceptedBodyPart;
claim.PGCase = pGCase;
claim.EmployersDefenseAttorney = employersDefenseAttorney;
claim.AccidentDescription = accidentDescription;
claim.ClaimExaminerFirstName = claimExaminerFirstName;
claim.ClaimExaminerLastName = claimExaminerLastName;
claim.ClaimExaminerEmail = claimExaminerEmail;
claim.ClaimantAttorney = claimantAttorney;
claim.WorkerId = workerId;
claim.WorkerType = workerType;
return claim;
}

更新:添加了 Paul 的建议代码作为试用。这确实有效。

whereClause = string.Format("ClaimantLastName = \"{0}\" and ClaimantSSN = \"{1}\"", lastName, ssn);

List<URIntake.Claim> claims = new List<Claim>();
URIntake.Claim claim = new Claim();
claim.ClaimantFirstName = "Jay";
claim.ClaimantLastName = "Williams";
claim.ClaimantSSN = "654219870";
claim.ClaimantDateOfBirth = new DateTime(1993, 1, 2);
claims.Add(claim);

claim = new Claim();
claim.ClaimantFirstName = "Santa";
claim.ClaimantLastName = "Claus";
claim.ClaimantSSN = "012345678";
claim.ClaimantDateOfBirth = new DateTime(1893, 1, 2);
claims.Add(claim);

List<AdvancedSearchResult> selectedClaims = claims.AsQueryable().Where(whereClause).Select(
u => new AdvancedSearchResult
{
ClaimNumber = u.ClaimNumber,
DateOfBirth = u.ClaimantDateOfBirth,
DateOfInjury = u.DateOfInjury,
Denied = u.Compensability == "Denied"
}).ToList();

最佳答案

这是一个使用 System.Linq.Expressions 的例子.虽然这里的示例特定于您的 Claim 类,但您可以创建类似此通用的函数,然后使用它们为您的所有实体动态构建谓词。我最近一直在使用为用户提供在任何实体属性(或属性组)上灵活搜索实体的功能,而无需对所有查询进行硬编码。

public Expression<Func<Claim, Boolean>> GetClaimWherePredicate(
String name,
String ssn)
{
//the 'IN' parameter for expression ie claim=> condition
ParameterExpression pe = Expression.Parameter(typeof(Claim), "Claim");

//Expression for accessing last name property
Expression eLastName = Expression.Property(pe, "ClaimantLastName");

//Expression for accessing ssn property
Expression eSsn = Expression.Property(pe, "ClaimantSSN");

//the name constant to match
Expression cName = Expression.Constant(name);

//the ssn constant to match
Expression cSsn = Expression.Constant(ssn);

//the first expression: ClaimantLastName = ?
Expression e1 = Expression.Equal(eLastName, cName);

//the second expression: ClaimantSSN = ?
Expression e2 = Expression.Equal(eSsn, cSsn);

//combine them with and
Expression combined = Expression.And(e1, e2);

//create and return the predicate
return Expression.Lambda<Func<Claim, Boolean>>(
combined,
new ParameterExpression[] { pe });
}

关于linq - 在 LINQ to EF 查询中构建动态 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14901430/

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