Getting started with LINQ
LINQ is nothing but an acronym of Language Integrated Query that is part of programming language. LINQ provides an easy way to write or query data with a specified syntax like we would use the where clause when trying to query data for some specific criteria. So, we can say that LINQ is a syntax that is used to query data.
In this section, we will see a simple example to query data. We have Person
list and the following code snippet provides us a various way to query data:
private static void TestLINQ() { var person = from p in Person.GetPersonList() where p.Id == 1 select p; foreach (var per in person) { WriteLine($"Person Id:{per.Id}"); WriteLine($"Name:{per.FirstName} {per.LastName}"); WriteLine($"Age:{per.Age}"); } }
In the preceding code snippet, we are querying List
of persons for personId =1. The LINQ query returns a result of IEnumerable<Person>
type which can be easily accessed...