Lambda expression with empty guy ()

I came across code like:

var vpAlias = null; var prices = session.QueryOver<Vehicle>() .Left.JoinAlias<VehiclePrice>(x => x.VehiclePrice, () => vpAlias, x => x.VehiclePriceTypeId == 1) .Where(() => vpAlias.Id == null || vpAlias.VehiclePriceTypeId == 1) .Select(x => x.Id, () => vpAlias.Price) .ToList(); 

which uses () in its lambda expressions. What does it mean? Is it used as a placeholder?

+7
c # lambda linq
source share
4 answers

It just means that this is an empty parameter list - for delegate types that have no parameters.

The fact that you can write x => x.Id is actually just a shorthand for (x) => x.Id , which in turn is a shorthand for (Vehicle x) => x.Id This is just a list of options.

+14
source share

This is the same as empty () in:

 static SomeType YourNamedMethod() { return vpAlias; } 
+3
source share

The parentheses contain a list of parameters. Only if you have only one parameter, you can leave them. Examples:

0 parameters: () => result

1 parameter: (x) => result or x => result

2 parameters: (x, y) => x + y;

+3
source share

() => { ... } is Func<TOut> , so it does not accept anything as input, you can see it as

 <returnType> MyFunction () { // Code goes here } 
+1
source share

All Articles