LINQ 101 error

M

Mike P

I am working through the LINQ 101 examples, and have found another
error, for the FirstIndexed example :

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

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

lbl.Text = evenNum + " is an even number at an even position
within the list.";

The error is : Delegate 'System.Func<int,bool>' does not take '2'
arguments. Does anybody know the correct syntax?
 
M

Marc Gravell

I think you now need to use the "int,int,bool" predicate first:

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

The First extension method now only has a parameterless overload and a
predicate (Func<T,bool>) overload - not an index-based predicate overload.

Marc
 
C

Chris Dunaway

I am working through the LINQ 101 examples, and have found another
error, for the FirstIndexed example :

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

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

lbl.Text = evenNum + " is an even number at an even position
within the list.";

The error is : Delegate 'System.Func<int,bool>' does not take '2'
arguments. Does anybody know the correct syntax?

*** Sent via Developersdexhttp://www.developersdex.com***

The First extension method only takes a delegate that has a single
integer parameter and returns bool. You are passing a delegate that
take 2 integer arguments and returns bool. They must have a mistake
in the example. Perhaps this will be closer to what they intended:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var evenNum = numbers.Select((num, index) => (num % 2 ==
0) && (index % 2 == 0));

Chris
 

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

Similar Threads


Top