我试图在一个通过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,
.
.
.
我想做的事情可行吗?看起来很简单。我哪里做错了?
UPDATE:这是索赔实体。
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();
5条答案
按热度按时间vwhgwdsa1#
这里是一个使用System.Linq.Expressions的例子。虽然这里的例子是特定于你的
Claim
类的,但是你可以使这样的函数通用,然后使用它们为你的所有实体动态地构建 predicate 。我最近一直在使用为用户提供一个灵活的搜索实体的任何实体属性(或属性组)函数,而不必硬编码所有的查询。umuewwlo2#
我个人真的很喜欢使用PredicateBuilder,因为它仍然有LINQ的外观和感觉,而不是一些神奇的字符串。你可以有各种各样的条件,然后添加子句到 predicate ,它会为你编译得很好,很干净。
http://www.albahari.com/nutshell/predicatebuilder.aspx
下面是我自己代码中的一个片段:
llmtgqce3#
您可以/需要使用动态链接
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
第一个月
看起来是如何处理的(注意不需要string.Format)
ahy6op9u4#
你可以调用任何类
最后感谢**bmused**
qlvxas9a5#
最简单方法是将LINQExtension与LINQKIT一起使用