Unexpected results in Linq query always + 1

I have the code below and I expect a different result.
I ruled out the following result: 100, 110, 120, 200, 500

using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication11 { public class Program { static void Main(string[] args) { var values = new List<int> { 100, 110, 120, 200 , 500 }; // In my mind the result shall be like (100,0) => 100 + 0 = 100 // In my mind the result shall be like (110,0) => 110 + 0 = 110 etc. var sumLinqFunction = new Func<int, int, int>((x, y) => x + y); var outputs = values.Select(sumLinqFunction); foreach (var o in outputs) Console.WriteLine(o); } } } 

Console exit (real result)

 100 111 122 203 504 

Now I'm wondering about this +1 where?

+7
c # linq
source share
5 answers

This is because the second Select () parameter overloads the method you use is only the index of the element in the input set.

EDIT:

As suggested by @ user7116, you can simply include a value from the outer scope in your projection function:

 int y = 0; var results = values.Select(x => x + y); 
+17
source share

Your Func , sumLinqFunction expects two parameters, adds them and returns the result.

When you use this in a Select statement, it looks like this:

 var outputs = values.Select((x, y) => x + y); 

which uses Select overload, which expects the second parameter to be an index.

The first argument to the selector represents the item to process. The second argument to the selector represents the zero index of this element in the original sequence. This can be useful if the elements are in a known order, and you want to do something with, for example, an element at a specific index. It can also be useful if you want to get the index of one or more elements.

So y starts at 0 . With each iteration, index ( y ) moves on to the next element, so you see +1 increment.

To explain this a bit, you can modify your Func to display the current x and y values, for example:

 var sumLinqFunction = new Func<int, int, int>((x, y) => { Console.WriteLine("x: {0} , y: {1}", x, y); return x + y; }); var outputs = values.Select(sumLinqFunction).ToList(); //iterate to get the results. 

This will give you:

 x: 100 , y: 0 x: 110 , y: 1 x: 120 , y: 2 x: 200 , y: 3 x: 500 , y: 4 

For your comment:

I need to call select with a default value of 0, not an index!

Then, instead of a Func you can simply do:

 int y = 0; var outputs = values.Select(r => r + y); 
+7
source share

You are using the wrong select overload.

It just adds an index to the current value:

 100 + 0 110 + 1 120 + 2 200 + 3 500 + 4 

You do not need a query for the expected output.

 var values = new List<int> { 100, 110, 120, 200 , 500 }; foreach (var o in outputs) Console.WriteLine(o); 
+4
source share

Check the definition of the overload you are calling:

http://msdn.microsoft.com/en-us/library/bb534869.aspx

In this case, you cause this overload:

 public static IEnumerable<TResult> Select<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, int, TResult> selector ) 

bringing the selector (focus)

Conversion function applied to each source element; the second parameter of the function is the index of the source Element .

So your lambda selector returns the number in the current index of the PLUS array, the index of the array, therefore

(x, y) => x + y

(100,0) => 100 + 0

(110.1) => 110 + 1

(120.2) => 120 + 2

(200.3) => 200 + 3

(500.4) => 500 + 4

+4
source share

If you want to use some function with the default parameter value:

  var values = new List<int> { 100, 110, 120, 200 , 500 }; var sumLinqFunction = new Func<int, int, int>((x, y) => x + y); var outputs = values.Select(i => sumLinqFunction(i, 0)); 
+1
source share

All Articles