LINQ 101 error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    LINQ 101 error

    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<in t,bool>' does not take '2'
    arguments. Does anybody know the correct syntax?

    *** Sent via Developersdex http://www.developersdex.com ***
  • Marc Gravell

    #2
    Re: LINQ 101 error

    I think you now need to use the "int,int,bo ol" 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

    Comment

    • Chris Dunaway

      #3
      Re: LINQ 101 error

      On Aug 22, 8:32 am, Mike P <mike.p...@gmai l.comwrote:
      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<in t,bool>' does not take '2'
      arguments. Does anybody know the correct syntax?
      >
      *** Sent via Developersdexht tp://www.developersd ex.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

      Comment

      Working...