需要初始化一个整数linq表达式

时间:2018-07-23 15:50:08

标签: c# linq expression

我正在使用表达式(System.Linq.Expressions.Expression),我需要初始化一个整数常量,但是我无法使其正常工作。

如果整数可以为空,这会很好:

Expression cnst;
cnst = Expression.Constant(new Nullable<int>([int value here]), typeof(Nullable<int>));

但是现在我需要将常量设为int,而不是Nullable。但是,如果我使用new int(),则无法初始化常量。 有什么想法吗? 我该怎么办?

1 个答案:

答案 0 :(得分:4)

我怀疑您错过了可以输入所需值的方法:

Expression cnt = Expression.Constant(42, typeof(int));

请注意,这也是将其写为可空值的更简单方法:

// This is a nullable integer constant expression, with the value 42
Expression cnt = Expression.Constant(42, typeof(int?));
相关问题