Bagikan melalui


Expression.Condition Metode

Definisi

ConditionalExpression Membuat yang mewakili pernyataan bersyarah.

Overload

Condition(Expression, Expression, Expression)

ConditionalExpression Membuat yang mewakili pernyataan bersyarah.

Condition(Expression, Expression, Expression, Type)

ConditionalExpression Membuat yang mewakili pernyataan bersyarah.

Condition(Expression, Expression, Expression)

Sumber:
ConditionalExpression.cs
Sumber:
ConditionalExpression.cs
Sumber:
ConditionalExpression.cs

ConditionalExpression Membuat yang mewakili pernyataan bersyarah.

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

Parameter

test
Expression

Untuk Expression mengatur properti yang Test sama dengan.

ifTrue
Expression

Untuk Expression mengatur properti yang IfTrue sama dengan.

ifFalse
Expression

Untuk Expression mengatur properti yang IfFalse sama dengan.

Mengembalikan

ConditionalExpression yang memiliki NodeType properti yang sama dengan Conditional dan Testproperti , , IfTruedan IfFalse diatur ke nilai yang ditentukan.

Pengecualian

test atau ifTrue atau ifFalse adalah null.

test. Jenis bukan Boolean.

-atau-

ifTrue. Jenis tidak sama dengan ifFalse. Jenis.

Contoh

Contoh kode berikut menunjukkan cara membuat ekspresi yang mewakili pernyataan bersyarah. Jika argumen pertama mengevaluasi ke true, argumen kedua dijalankan; jika tidak, argumen ketiga dijalankan.

// 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

Keterangan

Properti Type dari yang dihasilkan ConditionalExpression sama dengan Type properti .ifTrue

Lihat juga

Berlaku untuk

Condition(Expression, Expression, Expression, Type)

Sumber:
ConditionalExpression.cs
Sumber:
ConditionalExpression.cs
Sumber:
ConditionalExpression.cs

ConditionalExpression Membuat yang mewakili pernyataan bersyarah.

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

Parameter

test
Expression

Untuk Expression mengatur properti yang Test sama dengan.

ifTrue
Expression

Untuk Expression mengatur properti yang IfTrue sama dengan.

ifFalse
Expression

Untuk Expression mengatur properti yang IfFalse sama dengan.

type
Type

A Type untuk mengatur properti yang Type sama dengan.

Mengembalikan

ConditionalExpression yang memiliki NodeType properti yang sama dengan Conditional dan Testproperti , , IfTruedan IfFalse diatur ke nilai yang ditentukan.

Keterangan

Metode ini memungkinkan secara eksplisit menyatukan jenis hasil ekspresi kondisional dalam kasus di mana jenis ifTrue ekspresi dan ifFalse tidak sama. Jenis dan ifTrueifFalse harus secara implisit mereferensikan yang dapat ditetapkan ke jenis hasil. diperbolehkan type untuk menjadi Void.

Berlaku untuk