gpt4 book ai didi

c# - 需要初始化一个整型linq表达式

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:57 25 4
gpt4 key购买 nike

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

如果整数可以为空,则此方法工作正常:

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

但现在我需要常量只是 int,而不是 Nullable。但是如果我使用 new int() 我不能初始化常量。对此有什么想法吗?我该怎么做?

最佳答案

我怀疑你错过了你可以只输入你想要的值:

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?));

关于c# - 需要初始化一个整型linq表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51482781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com