C# 带空参数的Lambda表达式()

C# 带空参数的Lambda表达式(),c#,linq,lambda,C#,Linq,Lambda,我遇到了这样的代码: var vpAlias = null; var prices = session.QueryOver<Vehicle>() .Left.JoinAlias<VehiclePrice>(x => x.VehiclePrice, () => vpAlias, x => x.VehiclePriceTypeId == 1) .Where(() => vpAlias.Id == null || vpAlias.Vehi

我遇到了这样的代码:

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();
var-vpAlias=null;
var prices=session.QueryOver()
.Left.JoinAlias(x=>x.VehiclePrice,()=>vpAlias,x=>x.VehiclePriceTypeId==1)
.其中(()=>vpAlias.Id==null | | vpAlias.VehiclePriceTypeId==1)
.Select(x=>x.Id,()=>vpAlias.Price)
.ToList();

在其lambda表达式中使用
()
。这是什么意思?它只是用作占位符吗?

它只是意味着它是一个空的参数列表-对于没有任何参数的委托类型

您可以编写
x=>x.Id
的事实实际上只是
(x)=>x.Id
的简写,而这又是
(Vehicle x)=>x.Id
的简写。它只是一个参数列表。

()=>{…}
是一个
函数,因此它不接受任何输入,您可以将其视为

<returnType> MyFunction () { // Code goes here }
MyFunction(){//code在这里运行}

它与中的空
()
相同:

static SomeType YourNamedMethod()
{
   return vpAlias;
}

括号包含参数列表。只有当只有一个参数时,才能将其禁用。 示例:

0参数:
()=>结果

1参数:
(x)=>result或x=>result


2个参数:
(x,y)=>x+y

var vpAlias=null是不允许的!另外:不一定是
Func
它可以是任何类型。例如
Action
。没错,但在OP的问题中,它总是起作用的,所以我没有采取行动:)解释得很好,先生……我是。没有意识到这一点。这在你的书中提到过吗?@EhsanSajjad:Lambda表达式是通用的(第9.1节)。我不确定我是否特别提到空参数列表。你能告诉我在哪种情况下传递空参数吗?@EhsanSajjad:这不是“传递”空参数的问题,而是你想在任何时候使用不带参数的委托,例如
Func