sideburns_01
Passing Lambda as a method parameter: Some issues
ok, thanks for the reference. I was trying to build something like this using expressions rather than sql syntax
private sealed class referenceValue<T>
{
public int Value {get; set;}
}
public static IQueryable<T> WithIdExtension<T>(this IQueryable<T> query) where T : class
{
//entity we are applying filter to
var memberEntity = Expression.Parameter(typeof(T))
//the property we want to access
var accessEntityProperty = Expression.PropertyOrField(memberEntity, "SpecificId')
var objectValue = new referenceValue<T> {Value = someInternalMethod.GetValue()}
//this will parameterize value when converted to sql
var value = Expression.PropertyOrField(Expression.Constant(objectValue, "Value");
var equalExpression = Expression.Equal(accessEntityParameter, value)
Expression<Func<T, bool>> whereLambda = Expression.Lambda<Func<T, bool>>(equalExpression, memberEntity)
return query.Where(whereLambda)
}
9 replies
Passing Lambda as a method parameter: Some issues
Hi. thanks for getting back to me. What I'm trying to do is create a dynamic extension query that can encapsulate the where statement in
_context.ClassA.Where(a => a.ClassB.someVal == someReferenceVal)
. I was working with Expressions trees, and the problem there is getting the nested object and its propery to build that where clause. Not sure how to go about it. Though I am open to other ideas if my initial idea is wrong. The key idea being what options exist to encapsulate the .Where(a => a.ClassB.someVal == someReferenceVal)
9 replies