Search an element for predicate, and returns the last occurrence within List in CSharp
Description
The following code shows how to search an element for predicate, and returns the last occurrence within List.
Example
using System;/*from w w w . ja va 2s . c om*/
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
class Program
{
public static void Main(string[] args)
{
List<Book> Books = new List<Book>();
Book b = new Book();
b.Genre = "Computer";
Books.Add(b);
b = new Book();
b.Genre = "Computer";
Books.Add(b);
Book ndx = Books.FindLast(FindComputer);
Console.WriteLine(ndx);
}
private static bool FindComputer(Book bk)
{
if (bk.Genre == "Computer")
{
return true;
}else{
return false;
}
}
}
public class Book
{
public string Genre { get; set; }
}
The code above generates the following result.