In this post, let's see what let is in LINQ query-syntax queries. I think it's an overlooked feature in LINQ.
Let's consider the following query.
IQueryable<string> wfhEmployees = from e in context.Employees
where context.Departments
.Where(d => d.IsWfhAllowed)
.Select(d => d.Id)
.Contains(e.DepartmentId)
select e.FirstName + " " + e.LastName;
Here some might find it hard to understand the where condition immediately, we can
use the let clause to make it more readable.
IQueryable<string> wfhEmployees = from e in context.Employees
let wfhDepartments = context.Departments
.Where(d => d.IsWfhAllowed)
.Select(d => d.Id)
.ToList()
where wfhDepartments
.Contains(e.DepartmentId)
let fullName = e.FirstName + " " + e.LastName
select fullName;
Here I have used let to store the result of a subexpression to use it in subsequent clauses. This is really handy when you have complex queries, we can break it into multiple sub-expressions.
Hope this helps!
Happy Coding.
Regards,
Jaliya
No comments:
Post a Comment