我一直在使用LINQ进行查询,但我遇到了动态where子句的问题。我想检查条件,如果为true,则将其添加到我的查询的位置。问题是在哪里使用我的一个联接中的一个范围变量。我的工作查询如下:
var query = from project in db.ProjMasters
join pd in db.ProjDetails on project.ProjMasterID equals pd.ProjMasterID
join dc in db.DivCodes on project.DivisionCode equals dc.DivCode1
join ec in db.EmpCodes on project.ProjManager equals ec.UserNm
join ptc in db.ProjTypeCodes on pd.ProjTypeCode equals ptc.ProjTypeCode1
join psc in db.ProjStatusCodes on pd.ProjStatusCode equals psc.ProjStatusCode1
where pd.ProjDeleteDate == null
orderby project.Title
select new
{
project.ProjMasterID,
project.Title,
pd.ProjDesc,
pd.ContractNum,
pd.ProjDetailID,
dc.DivNm,
}
if (sTitle != null)
{
query = query.Where(x => x.Title.Contains(sTitle));
}
TypeDesc是db.ProjTypeCodes中的一个类型,所以我想说
if (sProjType != null)
{
query = query.Where(x => x.TypeDesc==sProjType);
}
但我只能在项目中使用类型的地方; " AnonymousType#1不包含' TypeDesc' ..."的定义。如何在ptc.TypeDesc上使用动态位置?
答案 0 :(得分:0)
问题在于您的订单,在您选择丢弃所有不在select语句中的数据时,如果您使用Inline if语句,那么您可以在第一个where子句中轻松地执行此操作,或者您可以在选择之后将选择分开您可能必须从Linq查询到linq语句
var query = from project in db.ProjMasters
join pd in db.ProjDetails on project.ProjMasterID equals pd.ProjMasterID
join dc in db.DivCodes on project.DivisionCode equals dc.DivCode1
join ec in db.EmpCodes on project.ProjManager equals ec.UserNm
join ptc in db.ProjTypeCodes on pd.ProjTypeCode equals ptc.ProjTypeCode1
join psc in db.ProjStatusCodes on pd.ProjStatusCode equals psc.ProjStatusCode1
where pd.ProjDeleteDate == null
&& sProjType!= null ? ptc.TypeDesc==sProjType): true
orderby project.Title
select new
{
project.ProjMasterID,
project.Title,
pd.ProjDesc,
pd.ContractNum,
pd.ProjDetailID,
dc.DivNm,
}
if (sTitle != null)
{
query = query.Where(x => x.Title.Contains(sTitle));
}
答案 1 :(得分:0)
使用IQueryable更多信息为link
ActionView::Helpers
答案 2 :(得分:0)
您的查询非常适合尝试将我最近发布的自定义扩展程序发布到"Nullable object must have a value" exception after checking for null on a non-primitive/non-struct object。
这个想法很简单。您可以直接在查询中构建动态条件,该方法将其转换为使用if
s:
var query = (from project in db.ProjMasters
join pd in db.ProjDetails on project.ProjMasterID equals pd.ProjMasterID
join dc in db.DivCodes on project.DivisionCode equals dc.DivCode1
join ec in db.EmpCodes on project.ProjManager equals ec.UserNm
join ptc in db.ProjTypeCodes on pd.ProjTypeCode equals ptc.ProjTypeCode1
join psc in db.ProjStatusCodes on pd.ProjStatusCode equals psc.ProjStatusCode1
where pd.ProjDeleteDate == null
&& (sProjType == null || ptc.TypeDesc == sProjType)
&& (sTitle == null || project.Title.Contains(sTitle))
orderby project.Title
select new
{
project.ProjMasterID,
project.Title,
pd.ProjDesc,
pd.ContractNum,
pd.ProjDetailID,
dc.DivNm,
}).ReduceConstPredicates();
P.S。如果链接出现问题,这是使用方法的源代码:
public static class QueryableExtensions
{
public static IQueryable<T> ReduceConstPredicates<T>(this IQueryable<T> source)
{
var reducer = new ConstPredicateReducer();
var expression = reducer.Visit(source.Expression);
if (expression == source.Expression) return source;
return source.Provider.CreateQuery<T>(expression);
}
class ConstPredicateReducer : ExpressionVisitor
{
private int evaluateConst;
private bool EvaluateConst { get { return evaluateConst > 0; } }
private ConstantExpression TryEvaluateConst(Expression node)
{
evaluateConst++;
try { return Visit(node) as ConstantExpression; }
catch { return null; }
finally { evaluateConst--; }
}
protected override Expression VisitUnary(UnaryExpression node)
{
if (EvaluateConst || node.Type == typeof(bool))
{
var operandConst = TryEvaluateConst(node.Operand);
if (operandConst != null)
{
var result = Expression.Lambda(node.Update(operandConst)).Compile().DynamicInvoke();
return Expression.Constant(result, node.Type);
}
}
return EvaluateConst ? node : base.VisitUnary(node);
}
protected override Expression VisitBinary(BinaryExpression node)
{
if (EvaluateConst || node.Type == typeof(bool))
{
var leftConst = TryEvaluateConst(node.Left);
if (leftConst != null)
{
if (node.NodeType == ExpressionType.AndAlso)
return (bool)leftConst.Value ? Visit(node.Right) : Expression.Constant(false);
if (node.NodeType == ExpressionType.OrElse)
return !(bool)leftConst.Value ? Visit(node.Right) : Expression.Constant(true);
var rightConst = TryEvaluateConst(node.Right);
if (rightConst != null)
{
var result = Expression.Lambda(node.Update(leftConst, node.Conversion, rightConst)).Compile().DynamicInvoke();
return Expression.Constant(result, node.Type);
}
}
}
return EvaluateConst ? node : base.VisitBinary(node);
}
protected override Expression VisitConditional(ConditionalExpression node)
{
if (EvaluateConst || node.Type == typeof(bool))
{
var testConst = TryEvaluateConst(node.Test);
if (testConst != null)
return Visit((bool)testConst.Value ? node.IfTrue : node.IfFalse);
}
return EvaluateConst ? node : base.VisitConditional(node);
}
protected override Expression VisitMember(MemberExpression node)
{
if (EvaluateConst || node.Type == typeof(bool))
{
var expressionConst = node.Expression != null ? TryEvaluateConst(node.Expression) : null;
if (expressionConst != null || node.Expression == null)
{
var result = Expression.Lambda(node.Update(expressionConst)).Compile().DynamicInvoke();
return Expression.Constant(result, node.Type);
}
}
return EvaluateConst ? node : base.VisitMember(node);
}
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (EvaluateConst || node.Type == typeof(bool))
{
var objectConst = node.Object != null ? TryEvaluateConst(node.Object) : null;
if (objectConst != null || node.Object == null)
{
var argumentsConst = new ConstantExpression[node.Arguments.Count];
int count = 0;
while (count < argumentsConst.Length && (argumentsConst[count] = TryEvaluateConst(node.Arguments[count])) != null)
count++;
if (count == argumentsConst.Length)
{
var result = Expression.Lambda(node.Update(objectConst, argumentsConst)).Compile().DynamicInvoke();
return Expression.Constant(result, node.Type);
}
}
}
return EvaluateConst ? node : base.VisitMethodCall(node);
}
}
}