Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Query syntax vs. Method Chain: return type

Tags:

c#

linq

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>

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.

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!

like image 867
Stefan_E Avatar asked Jan 03 '14 14:01

Stefan_E


People also ask

What is the return type of LINQ?

By default, LINQ queries return a list of objects as an anonymous type. You can also specify that a query return a list of a specific type by using the Select clause.

What is query syntax and method syntax in LINQ?

Query syntax and method syntax are semantically identical, but many people find query syntax simpler and easier to read. Some queries must be expressed as method calls. For example, you must use a method call to express a query that retrieves the number of elements that match a specified condition.

What is LINQ query syntax?

LINQ query syntax is consist of a set of query keywords defined into the . NET Framework version 3.5 or Higher. This allows the programmer or developers to write the commands similar to SQL style in the code(C# or VB.NET) without using quotes. It is also know as the Query Expression Syntax.

What do most LINQ operators return?

These operators return a Boolean value i.e. True or False when some or all elements within a sequence satisfy a specific condition.


1 Answers

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

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.

like image 103
MarcinJuraszek Avatar answered Sep 22 '22 12:09

MarcinJuraszek