How to implement method with expression parameter c# - c#

I want to create a method like this:
var result = database.Search<EntityType>(x=>x.Name, "Entity Name field value");
result = database.Search<EntityType>(x=>x.Id, "Entity Id field value");
result = database.Search<EntityType2>(x=>x.Id, "Entity2 Id field value");
result = database.Search<EntityTypeAny>(x=>x.FieldAny, "EntityAny FieldAny value");
How can I implement this method?

You can turn a selector and value into a predicate using Expression.Equal:
static IQueryable<TSource> Search<TSource, TValue>(
this IQueryable<TSource> source,
Expression<Func<TSource,TValue>> selector,
TValue value)
{
var predicate = Expression.Lambda<Func<TSource,bool>>(
Expression.Equal(
selector.Body,
Expression.Constant(value, typeof(TValue))
), selector.Parameters);
return source.Where(predicate);
}
Then you just need to do something like:
var result = database.SomeEntities.Search(x => x.SomeProp, "value");
If you want to do it from the database, then that depends on what the database is; for example, with LINQ-to-SQL you could add an additional method:
static IQueryable<TSource> Search<TSource, TValue>(
this System.Data.Linq.DataContext database,
Expression<Func<TSource, TValue>> selector,
TValue value) where TSource : class
{
IQueryable<TSource> source = database.GetTable<TSource>();
return Search(source, selector, value);
}
and use:
var result = database.Search<SomeEntity, string>(x => x.SomeProp, "value");
frankly I think it is clearer to use the database.SomeEntities version, though.

I can only think of this (with 2 generic arguments)
public static IEnumerable<TModel> Search<TModel, TValue>(
Expression<Func<TModel, TValue>> expression,
TValue value
)
{
return new List<TModel>();
}
usage
var result = Search<EntityType, int>(x => x.Id, 1);
var result2 = Search<EntityType, string>(x => x.Name, "The name");
you can replace TValue with object to avoid the second generic argument, but I would stick with this.
Btw. this works great in conjunction with this little helper
public static class ExpressionHelpers
{
public static string MemberName<T, V>(this Expression<Func<T, V>> expression)
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
throw new InvalidOperationException("Expression must be a member expression");
return memberExpression.Member.Name;
}
}
Now you can get the Name of the Property (Id oder Name) in this example by calling
var name = expression.MemberName();

do you want types to dynamic
public ReturnType Read<ReturnType>(string FieldName, object dfVal)
{
if (Res.IsDBNull(Res.GetOrdinal(FieldName)))
return dfVal;
try {
return (ReturnType)Res.GetValue(Res.GetOrdinal(FieldName));
} catch (Exception ex) {
return dfVal;
}
}

Related

Create lambda predicate expression to access object sub properties

I have a extension method which creates a lambda predicate expression, which I use to filter data when the value is not null.
It works fine for cases like
query.FilterBy(obj => obj.MyProp, value);
but it fails in cases like
query.FilterBy(obj => obj.MyObject.MyProp, value);
In this case the expression for MyProp property is accessed on obj, which of course doesn't have the property.
I'm not familiar with the expression tree and could not figure out how to modify the extension method to access properties of sub objects. Does anyone have an idea how I could do it?
The extension method:
public static IQueryable<TSource> FilterBy<TSource, TProp>(this IQueryable<TSource> source,
Expression<Func<TSource, TProp>> property,
TProp value)
{
if (value != null)
{
var parameter = Expression.Parameter(typeof(TSource));
var memberExpression = property.Body as MemberExpression ??
((UnaryExpression)property.Body).Operand as MemberExpression;
if (memberExpression is null)
{
throw new InvalidOperationException("Please provide a valid property expression.");
}
var propertyName = memberExpression.Member.Name;
var body = Expression.Equal(
Expression.Property(parameter, propertyName),
Expression.Constant(value));
var predicate = Expression.Lambda<Func<TSource, bool>>(body, parameter);
source = source.Where(predicate);
}
return source;
}
You could simply use epxression passed like parameter property of FilterBy() function for creating new filtering expression instead of trying to create expression from scratch. It will works with "sub-properties".
public static IQueryable<TSource> FilterBy<TSource, TProp>(this IQueryable<TSource> source,
Expression<Func<TSource, TProp>> property,
TProp value)
{
if (value != null)
{
var expression = Expression.Equal(property.Body, Expression.Constant(value));
var predicate = Expression.Lambda<Func<TSource, bool>>(expression, property.Parameters[0]);
source = source.Where(predicate);
return source;
}
return source;
}

Expression.Call GroupBy then Select and Count()?

Using Expression trees, I would need to build a GroupBy in a generic way.
The static method I'm going to use is the following:
public static IQueryable<Result> GroupBySelector<TSource>(this IQueryable<TSource> source, String coloumn)
{
//Code here
}
The Result class has two property :
public string Value { get; set; }
public int Count { get; set; }
Basically I'd like to build the following Linq query via Expression trees:
query.GroupBy(s => s.Country).Select(p => new
{
Value = p.Key,
Count = p.Count()
}
)
How would you implement it?
Looking at:
query.GroupBy(s => s.Country).Select(p => new
{
Value = p.Key,
Count = p.Count()
}
);
To match the signature of IQueryable<Result> what you actually need here is:
query.GroupBy(s => s.Country).Select(p => new
Result{
Value = p.Key,
Count = p.Count()
}
);
Now, the Select can work with any IQueryable<IGrouping<string, TSource>> as is. It's only the GroupBy that needs us to use expression trees.
Our task here is to start with a type and a string that represents a property (that itself returns string) and create a Expression<Func<TSource, string>> that represents obtaining the value of that property.
So, let's produce the simple bit of the method first:
public static IQueryable<Result> GroupBySelector<TSource>(this IQueryable<TSource> source, string column)
{
Expression<Func<TSource, string>> keySelector = //Build tree here.
return source.GroupBy(keySelector).Select(p => new Result{Value = p.Key, Count = p.Count()});
}
Okay. How to build the tree.
We're going to need a lambda that has a paramter of type TSource:
var param = Expression.Parameter(typeof(TSource));
We're going to need to obtain the property whose name matches column:
Expression.Property(param, column);
And the only logic needed in the lambda is simply to access that property:
Expression<Func<TSource, string>> keySelector = Expression.Lambda<Func<TSource, string>>
(
Expression.Property(param, column),
param
);
Putting it all together:
public static IQueryable<Result> GroupBySelector<TSource>(this IQueryable<TSource> source, String column)
{
var param = Expression.Parameter(typeof(TSource));
Expression<Func<TSource, string>> keySelector = Expression.Lambda<Func<TSource, string>>
(
Expression.Property(param, column),
param
);
return source.GroupBy(keySelector).Select(p => new Result{Value = p.Key, Count = p.Count()});
}
About the only thing left is the exception-handling, which I normally don't include in an answer, but one part of this is worth paying attention to.
First the obvious null and empty checks:
public static IQueryable<Result> GroupBySelector<TSource>(this IQueryable<TSource> source, String column)
{
if (source == null) throw new ArgumentNullException("source");
if (column == null) throw new ArgumentNullException("column");
if (column.Length == 0) throw new ArgumentException("column");
var param = Expression.Parameter(typeof(TSource));
Expression<Func<TSource, string>> keySelector = Expression.Lambda<Func<TSource, string>>
(
Expression.Property(param, column),
param
);
return source.GroupBy(keySelector).Select(p => new Result{Value = p.Key, Count = p.Count()});
}
Now, let's consider what happens if we pass a string for column that doesn't match a property of TSource. We get an ArgumentException with the message Instance property '[Whatever you asked for]' is not defined for type '[Whatever the type is]'. That's pretty much what we want in this case, so no issue.
If however we passed a string that did identify a property but where that property wasn't of type string we'd get something like "Expression of type 'System.Int32' cannot be used for return type 'System.String'". That's not dreadful, but it's not great either. Let's be more explicit:
public static IQueryable<Result> GroupBySelector<TSource>(this IQueryable<TSource> source, String column)
{
if (source == null) throw new ArgumentNullException("source");
if (column == null) throw new ArgumentNullException("column");
if (column.Length == 0) throw new ArgumentException("column");
var param = Expression.Parameter(typeof(TSource));
var prop = Expression.Property(param, column);
if (prop.Type != typeof(string)) throw new ArgumentException("'" + column + "' identifies a property of type '" + prop.Type + "', not a string property.", "column");
Expression<Func<TSource, string>> keySelector = Expression.Lambda<Func<TSource, string>>
(
prop,
param
);
return source.GroupBy(keySelector).Select(p => new Result{Value = p.Key, Count = p.Count()});
}
If this method was internal the above would perhaps be over-kill, but if it was public the extra info would be well worth it if you came to debug it.

Apply MemberExpression to an object to retrieve Property value

I am trying to write a generic function that takes a MemberExpression and an object and returns the value of the Property defined in the member expression.
Here's an example of the code to get the Property name.
public static TProperty GetPropertyName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression, TModel model)
{
if (expression.Body is MemberExpression)
{
return ((MemberExpression)expression.Body).Member.Name;
}
else
{
var op = ((UnaryExpression)expression.Body).Operand;
return ((MemberExpression)op).Member.Name;
}
}
But I want to retrieve the value of the property from the model:
public static string GetPropertyValue<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression, TModel model)
{
if (expression.Body is MemberExpression)
{
// how do I apply the expression.Body to get the value of the property from model??
}
else
{
var op = ((UnaryExpression)expression.Body).Operand;
return ((MemberExpression)op).Member.Name;
}
}
The way I call this function is:
GetPropertyValue<ObjectModel,bool>(m => m.somebool, m);
MemberExpression refers to MemberInfo, which will be PropertyInfo in case of property expression:
static class MemberExpressionHelper
{
public static TProperty GetPropertyValue<TModel, TProperty>(TModel model, Expression<Func<TModel, TProperty>> expression)
{
// case with `UnaryExpression` is omitted for simplicity
var memberExpression = (MemberExpression)expression.Body;
var propertyInfo = (PropertyInfo)memberExpression.Member;
return (TProperty)propertyInfo.GetValue(model);
}
}
Besides, it is more natural to swap parameters (first is model, second is expression). As a side effect, this allows compiler to infer type arguments:
var bar = MemberExpressionHelper.GetPropertyValue(foo, _ => _.Bar);

How to pass a lambda expression to a method expecting lambda expression?

I have an extension method to get a property name as string:
public static string GetPropertyName<T, TResult>(this T obj, Expression<Func<T, TResult>> propertyId)
{
return ((MemberExpression)propertyId.Body).Member.Name;
}
Now I have another method, expecting to pass in list (param) of this kind of property lamba expression.
I want this new method to reuse the 1st method, but can't figure out how to pass it over
public string Test<T>(params Expression<Func<T, object>>[] ps)
{
foreach (var p in ps)
{
var howToCodeThis = p.GetPropertyName(dummy => dummy);
expected usage:
var result = Test<Something>(request.Sorting
, x => x.prop1
, x => x.prop2
, x => x.prop3
);
Update:
Backs answer worked once I change my GetPropertyName to cater for UnaryExpression:
public static string GetPropertyName<T, TResult>(this T obj, Expression<Func<T, TResult>> propertyId)
{
if (propertyId.Body is MemberExpression)
return ((MemberExpression)propertyId.Body).Member.Name;
if (propertyId.Body is UnaryExpression)
{
var op = ((UnaryExpression)propertyId.Body).Operand;
return ((MemberExpression)op).Member.Name;
}
throw new NotImplementedException(string.Format("GetPropertyName - {0}", propertyId.Body.GetType().FullName));
}
var howToCodeThis = GetPropertyName(default(T), p);
OR
var howToCodeThis = default(T).GetPropertyName(p);
But I noteced, you don't use obj in GetPropertyName method.

C# Expression Tree Binding

So what I am trying to do is use expression trees to apply a predicate to each value in a collection (read map or list.All(predicate)). It appears that I am not getting the input parameter to the predicate bound to the value supplied by All, and I'm a little stuck. Here is the code (using linqpad) that I am working with::
public class SomeType
{
public IEnumerable<bool> Collection { get; set; }
}
void Main()
{
var list = new SomeType {
Collection = new List<bool> { true, true, true }
};
var functor = Compiler((SomeType t) => t.Collection, (bool x) => x);
functor(list).Dump();
}
MethodInfo FindMethod<TInput>(Type location, string name)
{
var handle = location
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(method => method.Name == name).First();
return handle.MakeGenericMethod(typeof(TInput));
}
Predicate<TObject> Compiler<TObject, TProperty>(
Expression<Func<TObject, IEnumerable<TProperty>>> selector,
Expression<Predicate<TProperty>> predicate)
{
var query = FindMethod<TProperty>(typeof(Enumerable), "All");
var expression = Expression.Call(query,
new Expression[] {
Expression.Invoke(selector, selector.Parameters),
Expression.Lambda<Func<TProperty, bool>>(predicate.Body,
Expression.Parameter(typeof(TProperty))),
});
return Expression.Lambda<Predicate<TObject>>(expression,
selector.Parameters).Compile();
}
Thanks and sorry if this was answered in another question (I looked for a while).
This does work, but I had to change the Predicate<TObject> to Func<TObject, bool>. If you want I can try to change it back.
static Predicate<TObject> Compiler<TObject, TProperty>(
Expression<Func<TObject, IEnumerable<TProperty>>> selector,
Expression<Func<TProperty, bool>> predicate)
{
var query = FindMethod<TProperty>(typeof(Enumerable), "All");
var expression = Expression.Call(
query,
Expression.Invoke(selector, selector.Parameters),
predicate);
return Expression
.Lambda<Predicate<TObject>>(expression, selector.Parameters)
.Compile();
}
5 minutes later... And if you really want to use Predicate<TObject>...
static Predicate<TObject> Compiler<TObject, TProperty>(
Expression<Func<TObject, IEnumerable<TProperty>>> selector,
Expression<Predicate<TProperty>> predicate)
{
var query = FindMethod<TProperty>(typeof(Enumerable), "All");
var predicateAsFunc = Expression.Lambda<Func<TProperty, bool>>(
predicate.Body,
predicate.Parameters);
var expression = Expression.Call(
query,
Expression.Invoke(selector, selector.Parameters),
predicateAsFunc);
return Expression
.Lambda<Predicate<TObject>>(expression, selector.Parameters)
.Compile();
}

Categories

Resources