LastOrDefault method (objects with condition)

Description

The LastOrDefault method returns the last element of a sequence that satisfies a condition or a default value if no such element is found.

Sample

This sample gets the last Person, where the Age gerater then 30.

var data := List<Person>{}{ ;
   Person{}{Name := "Jon Doe", Age := 40}, ;
   Person{}{Name := "Jane Doe", Age := 20}, ;
   Person{}{Name := "Joe Schmoe", Age := 30} ;
}

var result := data:LastOrDefault({ q => q:Age > 30 })

Console.WriteLine(result != null)

Output

True

Complete sample

using System
using System.Linq
using System.Collections.Generic

procedure Execute() as void strict

   var data := List<Person>{}{ ;
      Person{}{Name := "Jon Doe", Age := 40}, ;
      Person{}{Name := "Jane Doe", Age := 20}, ;
      Person{}{Name := "Joe Schmoe", Age := 30} ;
   }

   var result := data:LastOrDefault({ q => q:Age > 30 })

   Console.WriteLine(result != null)
   return

class Person
   public property Name as string auto
   public property Age as int auto
end class