无法使用三元运算符来分配 Linq 表达式

发布于 2024-12-21 00:01:06 字数 438 浏览 0 评论 0原文

我刚刚输入了以下代码:

Expression<Func<ContentItem, bool>> expression = 
                fileTypeGroupID.HasValue ? n => n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value : n => true;

Visual Studio 说它无法推断 n 的类型。

该代码对我来说似乎很好 - 它只是使用三元运算符将两个 Expression 文字之一分配给 Expression 变量。

Visual Studio 是否不够智能,无法推断三元运算符内的 n 类型,或者我犯了某种错误?

I just typed the following code:

Expression<Func<ContentItem, bool>> expression = 
                fileTypeGroupID.HasValue ? n => n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value : n => true;

Visual Studio is saying it can't infer the type of n.

The code seems fine to me - it's just using a ternary operator to assign one of two Expression literals to an Expression variable.

Is Visual Studio just not smart enough to infer the type of n inside a ternary operator, or have I made some kind of mistake?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

々眼睛长脚气 2024-12-28 00:01:06

这个问题几乎每天都会以某种形式被问到。

条件运算符类型分析是从内部外部,而不是从外部内部。条件运算符不知道其结果被分配给什么类型,然后强制结果和这些类型的替代。它的作用恰恰相反;它计算出结果和替代的类型,采用这两种类型中更通用的类型,然后验证是否可以分配通用类型。

结果和替代项不包含有关 lambda 类型的信息,因此无法推断条件的类型。因此无法验证分配是否正确。

考虑为什么这种语言是这样设计的,是很有启发性的。假设您有重载:

 void M(Func<string, int> f) {}
 void M(Func<double, double> f) {}

和一个调用

M( b ? n=>n.Foo() : n => n.Bar() );

描述重载解析如何确定在从外到内推断类型的世界中选择 M 的哪个重载。

现在考虑一下这个:

M( b1 ? (b2 ? n=>n.Foo() : n => n.Bar() ) : (b3 ? n=>n.Blah() : n=>n.Abc()) );

变得更难不是吗?现在想象 Foo、Bar、Blah 和 Abc 本身就是采用 func 的方法,并且还具有包含 lambda 的条件运算符的参数。

我们不希望让类型推断过程变得如此复杂而没有相应的巨大好处,而条件运算符也没有如此巨大的好处。

在您的情况下,您应该做的是将结果和替代方案中的一个或两个强制转换为特定类型。

例子:

Expression<Func<ContentItem, bool>> expression = 
    fileTypeGroupID.HasValue
        ? (Expression<Func<ContentItem, bool>>)(n => 
            n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value;
        )
        : n => true;

This question is asked almost every day in some form.

The conditional operator type analysis proceeds from inside to outside, not outside to inside. The conditional operator does not know to what type its results are being assigned and then coerces the consequence and alternative to those types. It does the opposite; it works out the types of the consequence and alternative, takes the more general of those two types, and then verifies that the general type may be assigned.

The consequence and alternative contain no information about what the type of the lambda should be, and therefore the type of the conditional cannot be inferred. Therefore it cannot be verified that the assignment is correct.

It is edifying to consider why the language was designed that way. Suppose you have overloads:

 void M(Func<string, int> f) {}
 void M(Func<double, double> f) {}

and a call

M( b ? n=>n.Foo() : n => n.Bar() );

Describe how overload resolution determines which overload of M is chosen in a world where types are inferred from outside to inside.

Now consider this one:

M( b1 ? (b2 ? n=>n.Foo() : n => n.Bar() ) : (b3 ? n=>n.Blah() : n=>n.Abc()) );

Getting harder isn't it? Now imagine that Foo, Bar, Blah and Abc were themselves methods that took funcs, and also had arguments with conditional operators containing lambdas.

We do not wish to make the type inference process so complex without a corresponding huge benefit, and there is no such huge benefit for the conditional operator.

What you should do in your case is cast one or both of the consequence and alternative to the specific type.

Example:

Expression<Func<ContentItem, bool>> expression = 
    fileTypeGroupID.HasValue
        ? (Expression<Func<ContentItem, bool>>)(n => 
            n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value;
        )
        : n => true;
贱人配狗天长地久 2024-12-28 00:01:06

这并不能回答您为什么编译器无法推断类型的问题,但一个简单的解决方法是以这种方式编写表达式:

Expression<Func<ContentItem, bool>> expression = 
  n => !fileTypeGroupID.HasValue || n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value;

This doesn't answer you question of why the compiler could not infer the type, but an easy work around would be to write your expression this way:

Expression<Func<ContentItem, bool>> expression = 
  n => !fileTypeGroupID.HasValue || n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value;
简美 2024-12-28 00:01:06
fileTypeGroupID.HasValue ? (ContentItem n) => n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value
                         : (ContentItem n) => true;

请注意条件运算符的正确名称吗? :这是三元运算符的示例(并且是许多编程语言中唯一的三元运算符)。

fileTypeGroupID.HasValue ? (ContentItem n) => n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value
                         : (ContentItem n) => true;

Note the conditional operator is the correct name for ? : It's an example of a ternary operator (and is the only ternary operator in many programming languages).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文