How do I querying a list of objects in LINQ?

Category: System.Linq, viewed: 2K time(s).

In the following example we'll use LINQ for querying object from a collection. We create an object Book as our application model. After that we use LINQ to get some books that published on the year 2008.

At the first line of the Main method we preparing some books and add it to the collection object, this will be our query data source. After that we create a query to get the books, the query declared using the var keyword. This means that the data type of the query will be inferred to us.

Finally at the foreach loop the query will be executed, it will get the book that we've declared in the query from the datasource.

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

namespace Kodecsharp.Example.Linq
{
    public class ObjectQueryDemo
    {
        public static void Main(string[] args)
        {
            List<Book> books = new List<Book>();
            books.Add(new Book(
                "C# in Depth", 
                "Jon Skeet", 
                "Manning Publications Co.", 
                2008));
            books.Add(new Book(
                "C# Cookbook",
                "Jay Hilyard & Stephen Teilhet", 
                "O'Relly Media, Inc", 
                2004));
            books.Add(new Book(
                "Head First C#", 
                "Andrew Stellman & Jennifer Greene", 
                "O'Relly Media, Inc", 
                2008));

            var query = from book in books
                        where book.Year == 2008
                        select book;

            foreach (Book book in query)
            {
                Console.WriteLine(book.Title + ", " 
                    + book.Author + ", " 
                    + book.Publisher + ", " 
                    + book.Year);
            }

            IEnumerable<Book> selectedBook = 
                from book in books
                where book.Title.Equals("C# in Depth")
                select book;

            foreach (Book book in selectedBook) 
            {
                Console.WriteLine(book.Title + ", "
                    + book.Author + ", "
                    + book.Publisher + ", "
                    + book.Year);
            }

            Console.ReadLine();
        }
    }

    class Book
    {
        private string title;
        private string author;
        private string publisher;
        private int year;

        public Book(string title, string author, string publisher, int year)
        {
            this.title = title;
            this.author = author;
            this.publisher = publisher;
            this.year = year;
        }

        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }

        public string Author
        {
            get { return this.author; }
            set { this.author = value; }
        }

        public string Publisher
        {
            get { return this.publisher; }
            set { this.publisher = value; }
        }

        public int Year
        {
            get { return this.year; }
            set { this.year = value; }
        }
    }
}

On the second query on the example above we don't use the var keyword to infer the data type but we explicitly state the return type we wanted to be the IEnumerable<Book>. And you can also see that we use the String.Equals() method to filter the title of the book.

When we run the program we'll get the following result:

C# in Depth, Jon Skeet, Manning Publications Co., 2008
Head First C#, Andrew Stellman & Jennifer Greene, O'Relly Media, Inc, 2008
C# in Depth, Jon Skeet, Manning Publications Co., 2008
Powered by Disqus