简体   繁体   English

动态linq表达式查询,出现问题

[英]dynamic linq expression to query, getting issue

I an tryin to join 4 tables within a query as per requirement. 我尝试根据要求在查询中联接4个表。 where as I wanted to add the conditions in where clause dynamically so, i could able to do this for 2 table query as of now. 在哪里,我想动态地在where子句中添加条件,所以我现在可以对2个表查询执行此操作。 but this 4 table join is bit the complex join here. 但是这4个表的连接在这里有点复杂。 To extend the functionality i am using following code to add dynamic where clause : 为了扩展功能,我使用以下代码添加了动态where子句:

public static class Extensions
    {
        public static IQueryable<T> AddEqualityCondition<T, V>(this IQueryable<T> queryable,
          string propertyName, V propertyValue)
        {
            ParameterExpression pe = Expression.Parameter(typeof(T), "p");

   IQueryable<T> x = queryable.Where<T>(Expression.Lambda<Func<T, bool>>(Expression.Equal(Expression.Property(pe, typeof(T).GetProperty(propertyName)), Expression.Constant(propertyValue, typeof(V)), false, typeof(T).GetMethod("op_Equality")), new ParameterExpression[] { pe }));
            return (x);
        }
    }

// My code to add where conditions: //我的代码添加where条件:

Query is:
 var agrs = (from agr in _dbContext.Agreements
                                 join amdv in _dbContext.AgreementMetaDataValues on agr.AgreementID equals amdv.AgreementID 
                                 join emd in _dbContext.EntityMetadatas on amdv.AttributeId equals emd.AttributeId
                                 join et in _dbContext.Entities on agr.EntityID equals et.EntityId
                                 select new  agr, amdv,emd });

//Add dynamically where conditions:
 agrs = agrs.AddEqualityCondition("?????", "A83C82C5-F9D6-4833-A234-EBB5D971280C");

This is working for 2 table join not for more than that. 这适用于2表联接不超过。 because within complex query it is generating the Annonymouse object. 因为在复杂的查询中它正在生成Annonymouse对象。 so so what should i need to pass in place of "??????" 所以,我需要代替“ ??????”通过什么 marks...? 分数...? typically need to pass the property name as"agr.AgreementId" but here it is throwing the expression as "Value Canot be Null : propertyName" in extension class. 通常需要将属性名称作为“ agr.AgreementId”传递,但是在扩展类中,它会将表达式作为“ Value Canot be Null:propertyName”抛出。 Need more guidance for this ... 需要更多指导...

I think you might want to consider something like (as an additional overload): 我认为您可能想考虑类似的东西(作为额外的重载):

public static IQueryable<T> AddEqualityCondition<T, V>(
    this IQueryable<T> queryable,
    Expression<Func<T, V>> selector, V propertyValue)
{
    var lambda = Expression.Lambda<Func<T,bool>>(
       Expression.Equal(
           selector.Body,
           Expression.Constant(propertyValue, typeof(V)),
           false, typeof(T).GetMethod("op_Equality")),
        selector.Parameters);
    return queryable.Where(lambda);           
}

and using: 并使用:

agrs = agrs.AddEqualityCondition(x => x.agr.AgreementId, 
             "A83C82C5-F9D6-4833-A234-EBB5D971280C");

however! 然而! it is much easier to use just: 使用起来更加容易:

agrs = agrs.Where(x => x.agr.AgreementId ==
             "A83C82C5-F9D6-4833-A234-EBB5D971280C");

最好使用谓词构建器,即动态组合表达式谓词 ,它使您可以轻松地动态构建查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM