I an tryin to join 4 tables within a query as per requirement. 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. but this 4 table join is bit the complex join here. To extend the functionality i am using following code to add dynamic where clause :
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:
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. because within complex query it is generating the Annonymouse object. 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. Need more guidance for this ...