Query an array of object by its property value in CSharp

Description

The following code shows how to query an array of object by its property value.

Example


using System;//ww  w  . j a  va2  s .c  o m
using System.Collections.Generic;
using System.Linq;

public class Book {
    public String Title { get; set; }

    public override String ToString() {
        return Title;
    }
}

class Program {
    static void Main(string[] args) {
      Book[] books = {new Book { Title="A" },
                      new Book { Title="F" },
                      new Book { Title="E" } };
      var titles =books.Where(book => book.Title.Contains("A"))
                       .Select(book => book.Title);
      foreach(var t in titles){
         Console.WriteLine(t);
      }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ