简体   繁体   English

条件linq查询行为

[英]conditional linq query behavior

I have a feeling the title is misleading, please edit if you choose to. 我觉得标题有误导性,请选择编辑。 when I do this: 当我这样做时:

var q  = (condition)?(from ...Select(..)): (from.. Select(..));

I get a error at ":" saying 我在“:”错误提示

Type of conditional expression could not be determined because 
there is no implicit conversion between anonymous types. 

But if I do: 但是如果我这样做:

var b = some base linq query;
var q = (condition)?(use b here one way):(use b here differently);

no complaints. 没什么好抱怨的。 Why? 为什么? How is second way different? 第二种方式有何不同?

Edit: 编辑:

Everywhere, my final projections are the same. 在任何地方,我的最终预测都是相同的。 (Final .Select(....) everywhere has same fields) (最后的.Select(....)处都有相同的字段)

Edit2: 编辑2:

I apologize.. typo on my part. 我很抱歉..我的错字。 Select()s everywhere were not the same. 各地的Select()都不一样。 Method 1 works fine too if the projections 'match' 如果投影“匹配”,方法1也可以正常工作

If you did a cast after your LINQ query everything would be fine. 如果您在LINQ查询之后进行了强制转换,一切都会好起来的。

Its sort of the same problem that you get then you do something like 你遇到的同样的问题,然后做类似的事情

int? val = true ? 1 : null;

That won't work, but if you cast the null like this: 那是行不通的,但是如果您将null像这样转换:

int? val = true ? 1 : (int?)null;

It does. 是的

A conditional expression needs the two operands it's evaluating to be the same type. 条件表达式需要它评估的两个操作数为相同类型。 So if you do 所以如果你这样做

var a = (condition)? "A" : "B";

... both "A" and "B" are of the same type (string), so the result, a , will be of type string . ...“ A”和“ B”都是相同的类型(字符串),因此结果a将是string类型。

What your error message is telling you is that your two operands (the two from ... Select statements) evaluate to anonymous types, but not the same anonymous type, and it can't convert one anonymous type into another. 您的错误消息告诉您,您的两个操作数( from ... Select语句的两个)求值为匿名类型,但不是相同的匿名类型,并且无法将一种匿名类型转换为另一种匿名类型。 Thus it doesn't know what type the result, q , should be. 因此,它不知道结果q应该是什么类型。

While I am not 100% sure, I believe that even if your two expressions are exactly the same, they will be different anonymous types. 尽管我不确定100%,但我相信即使您的两个表达式完全相同,它们也将是不同的匿名类型。 At any rate, the fact that you got that error indicates that they are not the same anonymous type. 无论如何,您收到该错误的事实表明它们不是同一匿名类型。

With your second set of statements, you first set b to be equal to the result of a linq query. 使用第二组语句,首先将b设置为等于linq查询的结果。 Hence it has a type. 因此它具有一种类型。 Although your statement does say so, the fact that the code compiles implies that (use b here one way) and (use b here differently) return results of the same type. 尽管您的声明确实如此,但是代码编译的事实意味着(use b here one way) (use b here differently) (use b here one way)(use b here differently) (use b here one way) (use b here differently)返回相同类型的结果。 If they both return an instance of the same type as b , for example, they will be of the same type. 例如,如果它们都返回与b类型相同的实例,则它们将具有相同的类型。

It think the error message explains this: 它认为错误消息解释了这一点:

Type of conditional expression could not be determined because there is no implicit conversion between anonymous types. 由于匿名类型之间没有隐式转换,因此无法确定条件表达式的类型。

This 这个

var x = 0 < 2 ? new { a = 1 } : new { a = 2 }

would compile. 会编译。 but this 但是这个

var x = 0 < 2 ? new { a = 1 } : new { b = 2 };

would give the error above because {a=1} and {b=2} are not the same anonymous types. 会出现上述错误,因为{a=1}{b=2}不是相同的匿名类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM