Expression.Condition 方法

定义

创建一个表示条件语句的 ConditionalExpression

重载

Condition(Expression, Expression, Expression)

创建一个表示条件语句的 ConditionalExpression

Condition(Expression, Expression, Expression, Type)

创建一个表示条件语句的 ConditionalExpression

Condition(Expression, Expression, Expression)

Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs

创建一个表示条件语句的 ConditionalExpression

public:
 static System::Linq::Expressions::ConditionalExpression ^ Condition(System::Linq::Expressions::Expression ^ test, System::Linq::Expressions::Expression ^ ifTrue, System::Linq::Expressions::Expression ^ ifFalse);
public static System.Linq.Expressions.ConditionalExpression Condition (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse);
static member Condition : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.ConditionalExpression
Public Shared Function Condition (test As Expression, ifTrue As Expression, ifFalse As Expression) As ConditionalExpression

参数

test
Expression

要将 Expression 属性设置为与其相等的 Test

ifTrue
Expression

要将 Expression 属性设置为与其相等的 IfTrue

ifFalse
Expression

要将 Expression 属性设置为与其相等的 IfFalse

返回

一个 ConditionalExpression,其 NodeType 属性等于 Conditional,并且其 TestIfTrueIfFalse 属性设置为指定值。

例外

testifTrueifFalsenull

test.Type 不为 Boolean

- 或 -

ifTrue.Type 不等于 ifFalse.Type。

示例

下面的代码示例演示如何创建表示条件语句的表达式。 如果第一个参数的计算结果为 true,则执行第二个参数;否则,执行第三个参数。

// Add the following directive to your file:
// using System.Linq.Expressions;

int num = 100;

// This expression represents a conditional operation.
// It evaluates the test (first expression) and
// executes the iftrue block (second argument) if the test evaluates to true,
// or the iffalse block (third argument) if the test evaluates to false.
Expression conditionExpr = Expression.Condition(
                           Expression.Constant(num > 10),
                           Expression.Constant("num is greater than 10"),
                           Expression.Constant("num is smaller than 10")
                         );

// Print out the expression.
Console.WriteLine(conditionExpr.ToString());

// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(
    Expression.Lambda<Func<string>>(conditionExpr).Compile()());

// This code example produces the following output:
//
// IIF("True", "num is greater than 10", "num is smaller than 10")
// num is greater than 10
' Add the following directive to your file:
' Imports System.Linq.Expressions  

Dim num As Integer = 100

' This expression represents a conditional operation; 
' it will evaluate the test (first expression) and
' execute the ifTrue block (second argument) if the test evaluates to true, 
' or the ifFalse block (third argument) if the test evaluates to false.
Dim conditionExpr As Expression = Expression.Condition(
                            Expression.Constant(num > 10),
                            Expression.Constant("n is greater than 10"),
                            Expression.Constant("n is smaller than 10")
                        )

' Print the expression.
Console.WriteLine(conditionExpr.ToString())

' The following statement first creates an expression tree,
' then compiles it, and then executes it.       
Console.WriteLine(
    Expression.Lambda(Of Func(Of String))(conditionExpr).Compile()())

' This code example produces the following output:
'
' IIF("True", "num is greater than 10", "num is smaller than 10")
' num is greater than 10

注解

Type生成的 ConditionalExpression 的 属性等于 TypeifTrue属性。

另请参阅

适用于

Condition(Expression, Expression, Expression, Type)

Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs

创建一个表示条件语句的 ConditionalExpression

public:
 static System::Linq::Expressions::ConditionalExpression ^ Condition(System::Linq::Expressions::Expression ^ test, System::Linq::Expressions::Expression ^ ifTrue, System::Linq::Expressions::Expression ^ ifFalse, Type ^ type);
public static System.Linq.Expressions.ConditionalExpression Condition (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse, Type type);
static member Condition : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * Type -> System.Linq.Expressions.ConditionalExpression
Public Shared Function Condition (test As Expression, ifTrue As Expression, ifFalse As Expression, type As Type) As ConditionalExpression

参数

test
Expression

要将 Expression 属性设置为与其相等的 Test

ifTrue
Expression

要将 Expression 属性设置为与其相等的 IfTrue

ifFalse
Expression

要将 Expression 属性设置为与其相等的 IfFalse

type
Type

要将 Type 属性设置为与其相等的 Type

返回

一个 ConditionalExpression,其 NodeType 属性等于 Conditional,并且其 TestIfTrueIfFalse 属性设置为指定值。

注解

此方法允许在 和 ifFalse 表达式类型不相等的情况下显式统一条件表达式的结果类型ifTrue。 和 ifFalse 的类型ifTrue必须是可分配给结果类型的隐式引用。 type允许 为 Void

适用于