简体   繁体   English

Linq查询语法与方法链:返回类型

[英]Linq Query syntax vs. Method Chain: return type

Reading here and here , I understand query syntax and method extensions are pretty much a syntactic difference. 在这里这里阅读,我理解查询语法和方法扩展几乎是一种语法差异。 But now, I've a data structure containing measurement data and want determine percentile. 但现在,我有一个包含测量数据的数据结构,并希望确定百分位数。 I write: 我写:

var ds = (from device in dataSet.devices
          where !device.paramValues[i].Equals(double.NaN)
          select device.paramValues[i]).OrderBy(val => val);
double median = percentile(ds as IOrderedEnumerable<double>, 0.5);

and all works fine. 一切正常。 ds is of type System.Linq.OrderedEnumerable<double,double> ds的类型为System.Linq.OrderedEnumerable<double,double>

What confuses me is to write the whole thing in query syntax: 令我困惑的是用查询语法编写整个内容:

var ds = (from device in dataSet.devices
          where !device.paramValues[i].Equals(double.NaN)
          orderby device.paramValues[i]
          select device.paramValues[i]);
double median = percentile(ds as IOrderedEnumerable<double>, 0.5);

Now, ds is of type System.Linq.Enumerable.WhereSelectEnumerableIterator<Dataset.DeviceData,double> and the call to the percentile function fails. 现在,ds的类型为System.Linq.Enumerable.WhereSelectEnumerableIterator<Dataset.DeviceData,double>并且对百分位函数的调用失败。

Not sure yet what I'm missing here... - there is no praticular reason why I'd prefer the second syntax, but I'd like to understand the difference ... Thanks for your help! 还不确定我在这里缺少什么... - 没有特别的理由为什么我更喜欢第二种语法,但我想了解其中的差异......感谢您的帮助!

from device in dataSet.devices
where !device.paramValues[i].Equals(double.NaN)
select device.paramValues[i]

is transformed into methods as follows: 转化为如下方法:

dataSet.devices
       .Where(device => !device.paramValues[i].Equals(double.NaN))
       .Select(device => device.paramValues[i]);

Adding your OrderBy call you get 添加您的OrderBy电话

dataSet.devices
       .Where(device => !device.paramValues[i].Equals(double.NaN))
       .Select(device => device.paramValues[i])
       .OrderBy(val => val);

The other query 另一个查询

from device in dataSet.devices
where !device.paramValues[i].Equals(double.NaN)
orderby device.paramValues[i]
select device.paramValues[i];

is transformed to 变成了

dataSet.devices
       .Where(device => !device.paramValues[i].Equals(double.NaN))
       .OrderBy(device => device.paramValues[i])
       .Select(device => device.paramValues[i]);

As you can see, it's not exactly the same method chain, and that's because you get different object as a result. 正如您所看到的,它不是完全相同的方法链,那是因为您获得了不同的对象。 Content is the same, but the type returned is not. 内容是相同的,但返回的类型不是。

Look at the other answer first. 先看看另一个答案。 Here is how you do the same with query syntax: 以下是对查询语法的相同操作:

from device in dataSet.devices
where !device.paramValues[i].Equals(double.NaN)
select device.paramValues[i] into x //into syntax
orderby x
select x;

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

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