Querying data using LINQ
Let's take a look at how we can use LINQ to query data in our applications. The following code snippet illustrates how you can use LINQ to display the contents of an array:
String[] employees = {"Joydip", "Douglas", "Jini", "Piku", "Amal", "Rama", "Indronil"}; var employeeNames = from employee in employees select employee; foreach (var empName in employeeNames) Response.Write(empName);
Now, let's discuss how to use LINQ to query a generic list. Consider the following GenericEmployeeList
list:
public List<String> GenericEmployeeList = new List<String>() { "Joydip", "Douglas", "Jini", "Piku", "Rama", "Amal", "Indronil" };
You can use LINQ to query this list as shown in the following code snippet:
IEnumerable<String> employees = from emp in GenericEmployeeList select emp; foreach (string employee in employees) { Response.Write(employee); }
You can use conditions with your LINQ query as well. The following example...