Linq60

A

AA2e72E

At this site: http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
The example : First - Indexed

This sample prints the elements of an integer array that are both even and
at an even index within the array. It uses First with an index parameter to
find the desired numbers.

public void Linq60() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

int evenNum = numbers.First((num, index) => (num % 2 == 0) && (index % 2
== 0));

Console.WriteLine("{0} is an even number at an even position within the
list.", evenNum);
}

The line int evenNum = numbers.First((num, index) => (num % 2 == 0) &&
(index % 2 == 0));

Creates an error : Delegate 'System.Func<int,bool>' does not take '2'
arguments

How can I alter the offending line (still using LINQ) to reproduce the
result shown?

PS: !!! I don't understand the point of publishing samples that do NOT work-
there are several others like this at the site !!!
 
G

Göran Andersson

AA2e72E said:
At this site: http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
The example : First - Indexed

This sample prints the elements of an integer array that are both even and
at an even index within the array. It uses First with an index parameter to
find the desired numbers.

public void Linq60() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

int evenNum = numbers.First((num, index) => (num % 2 == 0) && (index % 2
== 0));

Console.WriteLine("{0} is an even number at an even position within the
list.", evenNum);
}

The line int evenNum = numbers.First((num, index) => (num % 2 == 0) &&
(index % 2 == 0));

Creates an error : Delegate 'System.Func<int,bool>' does not take '2'
arguments

How can I alter the offending line (still using LINQ) to reproduce the
result shown?

PS: !!! I don't understand the point of publishing samples that do NOT work-
there are several others like this at the site !!!

I don't know why the example is in there, there is no overload of the
First method that uses a delegate with an index. As the page is under
"future versions", perhaps the method had such an overload at a stage in
the development.

You can use Where and First to make the code work, but that's not really
the point of the example...

int evenNum = numbers.Where((num, index) => (num % 2 == 0) && (index % 2
 
A

AA2e72E

Thanks for the solution

int evenNum = numbers.Where((num, index) => (num % 2 == 0) && (index % 2
== 0)).First();

Note: there is a stray > on the continuation line of the above post that
should not be there.
Göran Andersson said:
AA2e72E said:
At this site: http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
The example : First - Indexed

This sample prints the elements of an integer array that are both even and
at an even index within the array. It uses First with an index parameter to
find the desired numbers.

public void Linq60() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

int evenNum = numbers.First((num, index) => (num % 2 == 0) && (index % 2
== 0));

Console.WriteLine("{0} is an even number at an even position within the
list.", evenNum);
}

The line int evenNum = numbers.First((num, index) => (num % 2 == 0) &&
(index % 2 == 0));

Creates an error : Delegate 'System.Func<int,bool>' does not take '2'
arguments

How can I alter the offending line (still using LINQ) to reproduce the
result shown?

PS: !!! I don't understand the point of publishing samples that do NOT work-
there are several others like this at the site !!!

I don't know why the example is in there, there is no overload of the
First method that uses a delegate with an index. As the page is under
"future versions", perhaps the method had such an overload at a stage in
the development.

You can use Where and First to make the code work, but that's not really
the point of the example...

int evenNum = numbers.Where((num, index) => (num % 2 == 0) && (index % 2
== 0)).First();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top