In a query expression, it is sometimes useful to store the result of a sub-expression in order to use it in subsequent clauses. You can do this with the let keyword, which creates a new range variable and initializes it with the result of the expression you supply. Once initialized with a value, the range variable cannot be used to store another value. However, if the range variable holds a queryable type, it can be queried.let clause defined as
let-clause ::= let itemName = selExpr
let us calculate once average salary and then compare it to last salary of each employee using let keyword
MySqlConnection conn = new MySqlConnection("SERVER=localhost; DATABASE=employees;UID=root;PASSWORD=");
var emp = new EmployeeDataContext(conn);
var lastSalaries = from s in emp.Salaries
orderby s.EmpNo ascending, s.ToDate descending
group s by s.EmpNo into grp
let o = grp.First()
let averageSalary = (from s in emp.Salaries select s.Salary1).Average()
select new { o.EmpNo, o.Salary1, MoreOrLess = (averageSalary > o.Salary1) };
foreach (object b in lastSalaries)
Console.WriteLine(b);
Listing 6.
No comments:
Post a Comment