LINQ to Objects: Query objects in LINQ

Language Integrated Query (LINQ, pronounced “link”) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages. The main difference between SQL and LINQ is that the “from” keyword comes before the “select” keyword because we must first define the object we want to operate on. The “select” clause tells the compiler what we wish to extract in this query.

List initialization:

List<Book> ListOfBooks = new List<Book>()
{
    new Book {name = "DaVinci Code"    , owner = "Alex" , date = 2002},
    new Book {name = "Angels and Demons", owner = "Jeff" , date = 2005},
    new Book {name = "The Last Mughal", owner = "Danny", date = 2001},
};

LINQ query:

IEnumerable<Book> QueryResult = from Book in ListOfBooks
                                select Book;

When we query things from objects (LINQ to Objects) our queries always return an “IEnumrable” list of objects. “IEnumerable” type is the kind of list that exposes the enumerator, which supports a simple iteration over a non-generic collection, and is the type of each entry in the list.

foreach(Book bk in QueryResult)
    Console.WriteLine(bk.name);
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker