3

Edit: Despite the upvotes I don't think this is a good question anymore (see various comments). Sorry for the wasted space, but unfortunately I lack the rep to delete my own post.

Is there a better way of creating a lambda (or perhaps a predicate or expression that is not a lambda) that returns either 1) The sole argument unchanged or 2) a constant value? I encounter this occasionally when using LINQ where a basic LINQ extension method requires a Func<x,y> but I only need the input argument or a constant.

In a two year-old question Jon Skeet asserted that there is no reduction for the identity function (see LINQ identity function?). Is same true of a constant expression? Has (or will) anything chang(ed) with .NET 4.5 or C#5?

Community
  • 1
  • 1
Joshua Honig
  • 12,925
  • 8
  • 53
  • 75
  • 1
    Forgive me if this is stupid to point out, but you know `Count` has an overload that doesn't need a predicate? – Rawling Mar 13 '12 at 13:50
  • Err...oops. Bad example. Removed for the moment. – Joshua Honig Mar 13 '12 at 13:52
  • 1
    Other than `Count` (that, as showed by @Aducci doesn't have this problem)... What other "problems" do you have? – xanatos Mar 13 '12 at 13:53
  • 1
    Okay, so not count, what other example do you have? – James Michael Hare Mar 13 '12 at 13:55
  • The answer really depends on the problem you are trying to solve. In some cases lambdas can be reduced to method groups if the lambda just passes the input straight to a method call, but hard to tell what you want. It almost sounds like you want the equivalent of a C++ argument binder. – James Michael Hare Mar 13 '12 at 13:57
  • I'm realizing now it's a bad question. I attempted to delete it but because there is an answer I cannot. – Joshua Honig Mar 13 '12 at 14:03
  • The short answer is: nothing has changed about this in C# 5. If you want this kind of conciseness with functions, use F# instead. – Mauricio Scheffer Mar 13 '12 at 14:09

2 Answers2

7

you don't need to specify a predicate for the Count method

.Count()
Aducci
  • 26,101
  • 8
  • 63
  • 67
  • 1
    Yeah, that was a bad example. But my question is about lambdas, not the Count method. – Joshua Honig Mar 13 '12 at 13:53
  • 1
    @jmh_gr - Could you provide another example. The only thing I can think of is if you are building up a predicate for the Where clause. – Aducci Mar 13 '12 at 13:58
  • I'm realizing now it's a bad question. I attempted to delete it but because there is an answer I cannot. – Joshua Honig Mar 13 '12 at 14:03
2

If you were looking for a kind of lambda constant, a regular method would be the closest candidate. Let us say a Func<string,bool> is required and you want it to return it true in any case. Instead of writing collection.SomeLinqMethod(s => true) you could create a static class with appropriate methods

public static class Expressions
{
    public static bool True(string s)
    {
        return true;
    }
}

Then you would write

var result = collection.SomeLinqMethod(Expressions.True);

You could also have generic methods

public static bool True<T>(T item)
{
    return true;
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188