Func<> add an extra parameter dynamically - c#

how can I add an extra parameter to my Func<> expression ? Something like:
Func<char, bool> myPredicate = (x) => (char.IsLetter(x) || x == 'X');
...
"abc".All(x => char.IsDigit(x) || myPredicate);
but I get an error
Operator '||' cannot be applied to operands of type 'bool' and Func< char, bool>

You need to invoke the myPredicate function like this:
"abc".All(x => char.IsDigit(x) || myPredicate(x));

Or, what about this other approach?
var text = "abc";
var predicates = new Func<char, bool>[] {
x => char.IsLetter(x) || x == 'X',
char.IsDigit
};
var result = predicates.Any(text.All);
// Outputs TRUE
Console.WriteLine(result);
Also, if you need to check many specific characters, you can create a charEquals with curried parameters:
var text = "abc";
// More type inference to generalize charEquals to just equals, please!
Func<char, Func<char, bool>> charEquals = ca => cb => ca == cb;
var predicates = new Func<char, bool>[] {
char.IsLetter,
charEquals('X'),
charEquals('Y'),
charEquals('Z'),
char.IsDigit
};
var result = predicates.Any(text.All);
Console.WriteLine(result);

Related

Set parameter of any Func<>

I have an object of any of the func types func<>, Func<,>, func<,,> ... And I'd like to replace one of the input parameters with a constant value.
eg:
object SetParameter<T>(object function, int index, T value){
//I don't know how to code this.
}
Func<int, String, String> function = (a, b) => a.ToString() + b;
object objectFunction = function;
object newFunction = SetParameter<int>(objectFunction, 0, 5);
// Here the new function should be a Func<String, String> which value "(b) => function(5, b)"
I already now how to get the type of the resulting function, but that does not really help me in implementing the desired behavior:
private Type GetNewFunctionType<T>(object originalFunction, int index, T value)
{
Type genericType = originalFunction.GetType();
if (genericType.IsGenericType)
{
var types = genericType.GetGenericArguments().ToList();
types.RemoveAt(index);
Type genericTypeDefinition = genericType.GetGenericTypeDefinition();
return genericTypeDefinition.MakeGenericType(types.ToArray());
}
throw new InvalidOperationException($"{nameof(originalFunction)} must be a generic type");
}
It's not quite clear what the purpose of your conversion is for, but wouldn't it be easier to avoid all the reflection. E.g.:
Func<int, string, string> func3 = (a, b) => a.ToString() + b;
Func<string, string> func3withConst = (b) => func3(10, b);
Since you are talking about a very limited scope (supporting just Func<TReturn>, Func<T1, TReturn> and Func<T1, T2, TReturn>) doing this through reflection is much more error prone and harder to read.
Just in case you need to use expression tree to build the function:
object SetParameter<T>(object function, int index, T value)
{
var parameterTypes = function.GetType().GetGenericArguments();
// Skip where i == index
var newFuncParameterTypes = parameterTypes.SkipWhile((_, i) => i == index).ToArray();
// Let's assume function is Fun<,,> to make this example simple :)
var newFuncType = typeof(Func<,>).MakeGenericType(newFuncParameterTypes);
// Now build a new function using expression tree.
var methodCallParameterTypes = parameterTypes.Reverse().Skip(1).Reverse().ToArray();
var methodCallParameters = methodCallParameterTypes.Select(
(t, i) => i == index
? (Expression)Expression.Constant(value, typeof(T))
: Expression.Parameter(t, "b")
).ToArray();
// func.Invoke(5, b)
var callFunction = Expression.Invoke(
Expression.Constant(function),
methodCallParameters);
// b => func.Invoke(5, b)
var newFunc = Expression.Lambda(
newFuncType,
callFunction,
methodCallParameters.OfType<ParameterExpression>()
).Compile();
return newFunc;
}
To use this:
Func<int, string, string> func = (a, b) => a.ToString() + b;
var newFunc = (Func<string, string>)SetParameter<int>(func, 0, 5);
// Output: 5b
Console.WriteLine(newFunc("b"));

Convert Func delegate to a string

Is there any way to convert an existing Func delegate to a string like that:
Func<int, int> func = (i) => i*2;
string str = someMethod(func); // returns "Func<int, int> func = (i) => i*2"
or at least smth close to it
I found a similar question here. It more or less boils down to:
By #TheCloudlessSky,
Expression<Func<Product, bool>> exp = (x) => (x.Id > 5 && x.Warranty != false);
string expBody = ((LambdaExpression)exp).Body.ToString();
// Gives: ((x.Id > 5) AndAlso (x.Warranty != False))
var paramName = exp.Parameters[0].Name;
var paramTypeName = exp.Parameters[0].Type.Name;
// You could easily add "OrElse" and others...
expBody = expBody.Replace(paramName + ".", paramTypeName + ".")
.Replace("AndAlso", "&&");
Console.WriteLine(expBody);
// Output: ((Product.Id > 5) && (Product.Warranty != False))
It doesn't return the Func<int, int> func = (i) => part like in your question, but it does get the underlying expression!

C# Linq OrderBy filtering null or empty values to be last

I try to make my custom orderby extension method, i successfully worked my code but in addition i want to list null or empty or zero values last in result, anyone can help me about that issue ?
Here is my extension method to orderby
public static IQueryable<T> OrderBy<T>(this IQueryable<T> q, string SortField, bool isAsc)
{
//var nullExpr = Expression.Constant(null, typeof(T));
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, SortField);
var exp = Expression.Lambda(prop, param);
string method = isAsc ? "OrderBy" : "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(mce);
}
Thanks in advance
The simplest way is to use
OrderBy(e => String.IsNullOrEmpty(e.TeamName)
This doesn't require any extension method or custom IComparer implementation etc.
var entries = repository.Race.Where(e => e.EventId == id)
.OrderBy(e => String.IsNullOrEmpty(e.TeamName))
.ThenBy(e => e.LastName)
.ThenBy(e => e.FirstName);
Without using an extension method....
Create a custom IComparer<string> to check the empty values before using the default String.Compare. The first checks will return -1 instead of 1 or 1 instead of -1, if using the standard string comparison.
/// <summary>
/// Returns -1 instead of 1 if y is IsNullOrEmpty when x is Not.
/// </summary>
public class EmptyStringsAreLast : IComparer<string>
{
public int Compare(string x, string y)
{
if (String.IsNullOrEmpty(y) && !String.IsNullOrEmpty(x))
{
return -1;
}
else if (!String.IsNullOrEmpty(y) && String.IsNullOrEmpty(x))
{
return 1;
}
else
{
return String.Compare(x, y);
}
}
}
Pass your EmptyStringsAreLast comparer into the OrderBy of Lambda expression. In this solution teams who have entered the race should appear alphabetical order, but the unaffiliated race entries should appear at then end.
var entries = repository.Race.Where(e => e.EventId == id)
.OrderBy(e => e.TeamName, new EmptyStringsAreLast())
.ThenBy(e => e.LastName)
.ThenBy(e => e.FirstName);
This answer is perhaps what you were originally looking for - using your generic extension method:
public static IQueryable<T> OrderByFieldNullsLast<T>(this IQueryable<T> q, string SortField, bool Ascending)
{
//We are rebuilding .OrderByDescending(p => p.SortField.HasValue).ThenBy(p => p.SortField)
//i.e. sort first by whether sortfield has a value, then by sortfield asc or sortfield desc
//create the expression tree that represents the generic parameter to the predicate
var param = Expression.Parameter(typeof(T), "p");
//create an expression tree that represents the expression p=>p.SortField.HasValue
var prop = Expression.Property(param, SortField);
var hasValue = Expression.Property(prop, "HasValue");
var exp = Expression.Lambda(hasValue, param);
string method = "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var orderByCallExpression = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
//now do the ThenBy bit,sending in the above expression to the Expression.Call
exp = Expression.Lambda(prop, param);
types = new Type[] { q.ElementType, exp.Body.Type };
method = Ascending ? "ThenBy" : "ThenByDescending";
var ThenByCallExpression = Expression.Call(typeof(Queryable), method, types,orderByCallExpression, exp);
return q.Provider.CreateQuery<T>(ThenByCallExpression);
}
Building on Dave Anson's answer, you can user Comparer.Create() to create the Comparer from a lambda. Here's an example that sorts unsorted by its myString string fields, with null or empty strings appearing last.
var sorted = unsorted.OrderBy(x => x.myString, Comparer<string>.Create((x, y) => {
if ( string.IsNullOrEmpty(y) && !string.IsNullOrEmpty(x)) return -1;
else if (!string.IsNullOrEmpty(y) && string.IsNullOrEmpty(x)) return +1;
else return string.Compare(x, y);
}))
(To put them first, switch the signs on the 1 constants)
it works for me:
private static IQueryable<T> GetOrderQuery<T>(this IQueryable<T> q, BaseFilterCollection filter)
{
q = q.OrderBy(GetExpression<T>(filter.SortField));
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, filter.SortField);
var exp = Expression.Lambda(prop, param);
string method = filter.SortDirection == SortDirectionType.Asc ? "ThenBy" : "ThenByDescending";
Type[] types = { q.ElementType, exp.Body.Type };
var rs = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(rs);
}
private static Expression<Func<T, bool>> GetExpression<T>(string sortField)
{
ParameterExpression param = Expression.Parameter(typeof(T), "p");
Expression prop = Expression.Property(param, sortField);
var info = typeof(T).GetProperty(sortField, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
Expression exp = Expression.Equal(prop, info.PropertyType.IsValueType
? Expression.Constant(Activator.CreateInstance(info.PropertyType))
: Expression.Constant(null));
return Expression.Lambda<Func<T, bool>>(exp, param);
}
You dont need to complicate, the easiest way is to do something like this:
YourList.OrderByDescending(x => string.IsNullOrEmpty(x.value)
Use OrderByDescending or OrderBy depending on if you want to see empty strings in the beginning or last.
Regards

Any suggest to store a value in a lambda expression

I'm trying to write an in-line function for count occurrences of a word in a string using lambda expressions recursively.
The function:
Func<string, string, int> getOccurrences = null;
getOccurrences = (text, searchTerm) =>
text.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) == -1
? 0
: getOccurrences(
text.Substring(
text.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase)
+ searchTerm.Length),
searchTerm) + 1;
The problem is that I'm call IndexOf method twice,
The first one is for recursive break condition and the second one is to get the value for add it.
Is there any suggest to call it once?
Thanks in advance.
If you don't mind a non-pure-function lambda you can do:-
Func<string, string, int> getOccurrences = null;
getOccurrences = (text, searchTerm) =>
{
int i = text.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase);
return i == -1 ? 0 : getOccurrences(i + searchTerm.Length), searchTerm) + 1;
}
You could do it like this:
Func<string, string, int> getOccurrences =
(text, searchTerm) => getOccurrencesInternal(
text,
searchTerm,
text.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase));
Func<string, string, int, int> getOccurrencesInternal = null;
getOccurrences = (text, searchTerm, index) =>
index == -1
? 0
: getOccurrencesInternal(
text.Substring(
index + searchTerm.Length),
searchTerm) + 1;
I suggest you make it a separate method
Func<string, string, int> getOccurrences = GetOccurrences;
private int GetOccurrences(string text, string searchTerm)
{
//...
}
or inline
Func<string, string, int> getOccurrences = delegate(string text, string searchTerm)
{
//...
};
with lambda syntax but just another way of writing the above
Func<string, string, int> getOccurrences = (string text, string searchTerm) =>
{
//...
};
You could use an additional, anonymous lambda and invoke it immediately. I'm not certain of the exact C# syntax, but it should look something like:
Func<string, string, int> getOccurrences = null;
getOccurrences = (text, searchTerm) =>
((index) =>
index == -1
? 0
: getOccurrences(text.Substring(index + searchTerm.Length),
searchTerm) + 1
)(text.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase))
My usual solution to these kind of problems, ignoring any optimizations, is just to remove the matching term and check any change in the resulting string's length.
Func<String, String, Int32> getOccurrences = (text, term) =>
(text.Length - text.Replace(term, "").Length) / term.Length;

Lambda Expressions and searching

Lets say i have a form which have the following :
Name:TextBox
Email:TextBox
Age:TextBox
now i want to Get customers Collection based on this filter textboxs
so i want to to use something like :
List<customer> customers = getCustomerswhere(c=>c.name == txtName.Text && Email == txtEmail.Text);
now of course i dont know which he will fill and which he wont so
if (txtName.Text.trim() != "")
//something like c=>c.Name == txtName.text;
if (txtEmail.Text.trim() != "")
//something like and c=>c.Email == txtEmail.text;
how do i do this ! i cant concatenate lambda expressions , i know i can use dynamic expressions but i think there is easier way ? any idea how to implement this ?
ok i tried this:
Func<Customer,bool > a = (bb) => bb.fullName == "asdfsd";
Func<Customer, bool> b = c => c.lastName == "sdas";
Func<Customer, bool> cc = c => a(c) && b(c);
now comes another problem
the method im passing CC to is expecting Expression<Func<T, bool>> expression
so it doesnt work gives me compile time error cant convert between types!
you can create some expressions like:
var a = c => c.name == txtName.Text;
var b = c => c.name == txtName.Text;
and then concatenate them like this:
var result = c => a(c) && b(c);
Like this:
Func<Customer, bool> predicate = c => true;
if (txtName.Text.Trim() != "")
predicate = Concatenate(predicate, c => c.Name == txtName.text);
if (txtEmail.Text.Trim() != "")
predicate = Concatenate(predicate, c => c.Email == txtEmail.text);
static Func<T, bool> Concatenate(Func<T, bool> a, Func<T, bool> b) {
return t => a(t) && b(t);
}
The Concatenate method must be a separate method because lambda expressions capture variables by reference.
The line
predicate = c => predicate(c) && c.Name == txtName.text;
will result in a stack overflow because the predicate variable will always refer to the latest immutable delegate instance that you assign to it.
return Customers.Where(c => (txtName.Text.Trim() == "" || c.Name == txtName.Text)
&& (txtEmail.Text.Trim() == "" || c.Email == txtEmail.Text));
In other words, only impose the 'name' condition if the 'name' box is filled out, and only impose the 'email' condition if the 'email' box is filled out.
Note that you can use the String.IsNullOrWhiteSpace method in .NET 4.0 instead of the Trim technique you have used.
Here is how i Implemented it:
public class LambdaCriteries<T> : List<Expression<Func<T, bool>>>
{
public Expression<Func<T, bool>> GetFinalLambdaExpression()
{
var par = Expression.Parameter(typeof(T));
var intial = Expression.Invoke(this.First(),par);
var sec = Expression.Invoke(this.Skip(1).First(),par);
BinaryExpression binaryExpression = Expression.And(intial, sec);
if (this.Count> 2)
{
foreach (var ex in this.ToList().Skip(2))
{
binaryExpression = Expression.And(binaryExpression, Expression.Invoke(ex, par));
}
return Expression.Lambda<Func<T, bool>>(binaryExpression,par);
}
else
{
return Expression.Lambda<Func<T, bool>>(binaryExpression,par);
}
}
}
and to use it :
if(txtId.text != "")
criteries.Add(v => v.Id == int.Parse(txtId.text));
if(txtName.text != "")
criteries.Add(v => v.Name == txtId.text);
and final expression :
var finalexp = criteries.GetFinalLambdaExpression();

Categories

Resources