Using LINQ to query business objects in more easily than the methods I have mentioned in the previous blogs.
As an example I will use LINQ to query the employee names for all the employees whose salary is greater than 2000 as follows.
List<'Employee'> _employees = new List<'Employee'>{
new Employee(1){ EmployeeName = "Prajeesh Prathap", Salary = 1900},
new Employee(2){ EmployeeName = "Jacob Chacko", Salary = 4500},
new Employee(3){ EmployeeName = "Sreejith P.R", Salary = 2344},
new Employee(4){ EmployeeName = "Steve Micheal", Salary = 1099},
new Employee(5){ EmployeeName = "Mary John", Salary = 990},
new Employee(6){ EmployeeName = "Steve Hayden", Salary = 4500},
new Employee(7){ EmployeeName = "Samual Johnes", Salary = 3420},
new Employee(8){ EmployeeName = "Joseph Micheal", Salary = 900}
};
var names = _employees.Where(emp => emp.Salary > 2000)
.OrderBy(emp => emp.EmployeeName)
.Select(emp => emp.EmployeeName.ToUpper);
Or
var names = from name in _employees
where name.Salary > 2000
orderby name.EmployeeName
select name.EmployeeName.ToUpper;
where, select, orderby etc are queryoperators in LINQ defined in the System.Linq.Enumerable class.
LINQ can also be used to query a collection inside another collection based on certain filter criteria. Here’s another example for that.
public class Author
{
public String FirstName { get; set; }
public String LastName { get; set; }
public String Address { get; set; }
}
public class Book
{
public String Name { get; set; }
public List
public String Technology { get; set; }
}
List<'Book'> _books = new List<'Book'>
{
new Book
{
Name = "CLR via C#",
Technology = ".Net",
Authors = new List
{
new Author{ FirstName = "Jeffrey", LastName = "Richter", Address = "" }
},
},
new Book{
Name = "Expert C# 2005 Business Objects",
Technology = ".Net, C#",
Authors = new List
{
new Author { FirstName = "Rockford", LastName = "Lhotka", Address = "" }
}
},
new Book{
Name = "Pro Service-Oriented Smart Clients with .NET 2.0",
Technology = ".NET 2.0",
Authors = new List
{
new Author { FirstName = "Sayed", LastName = "Hashimi", Address = "United States of America" },
new Author { FirstName = "Scott", LastName = "Steffan", Address = "" }
}
},
new Book{
Name = "Professional WinFX Beta",
Technology = "WPF, WCF, WF",
Authors = new List
{
new Author { FirstName = "Jean-Luc", LastName = "David", Address = "" },
new Author { FirstName = "Bill", LastName = "Ryan", Address = "Indianapolis," },
new Author { FirstName = "Ron", LastName = "DeSerranno", Address = "" },
new Author { FirstName = "Alexandra", LastName = "Young", Address = "Indianapolis," }
}
}
};
var authors = from book in _books
where book.Authors.Count > 1
from author in book.Authors
where author.Address != string.Empty
select author;
or
var authors = _books.Where(book => book.Authors.Count > 1)
.SelectMany(book => book.Authors)
.Where(author => author.Address != String.Empty);
Avoiding duplicate query results in LINQ
The Distinct operator is used for avoiding duplicate results in a LINQ to Object query, like many other query operators Distinct also does not have an equivalent keyword operator in C#. You have to use the methord call for including Distinct in your LINQ query.
Example:
var authors = _books.Where(book => book.Authors.Count > 1)
.SelectMany(book => book.Authors)
.Distinct()
.Where(author => author.Address != String.Empty);
We will see the application of other important query operators in LINQ in the next part of this series.
No comments:
Post a Comment