Can't use ternary operator to assign Linq expression

纵饮孤独 提交于 2019-12-29 01:15:26

问题


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?


回答1:


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.




回答2:


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;



回答3:


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).



来源:https://stackoverflow.com/questions/8506733/cant-use-ternary-operator-to-assign-linq-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!