Here's one of the reasons why I love LINQ when querying the database:
using (var ctx = new Entities())
{
var emp = from y in ctx.Employees
where y.Name == "Harold Javier"
select y;
foreach(var y in emp)
{
return x.ToList();
}
GridView1.DataSource = x.ToList();
GridView1.DataBind();
}
Short. Easy To Understand. Optimized. Fast.
Here are some other LINQ Expressions you can use in your small projects:
LINQ Query Expressions
from y in ctx.Employees
where y.Salary > 100000m
select y;
from y in ctx.Employees
where y.Name == "Harold Javier"
select y;
from y in ctx.Employees
where y.Salary > 100000m && y.Name.StartsWith ("Ha")
select y;
from y in ctx.Employees
orderby y.Name, y.Age descending
select y;
from y in ctx.Employees
group y by y.Name into uniqueName
select uniqueName.FirstOrDefault();
(from y in ctx.Employees
join z in ctx.Accounts
on y.EmployeeId equals z.EmployeeId
select new
{
y.Name, y.Age, z.AccountName
}
);
LINQ Lambda Expressions
ctx.Employees.Select(y=>y);
ctx.Employees.Select(y=> new {y.LastName, y.FirstName});
ctx.Employees.OrderBy(y=>y.FirstName).ThenByDescending(y=>y.Salary);
ctx.Employees.Where(y=>y.Salary > 50000.00m && y.FirstName == "Harold");
ctx.Employees.Where(y+y.DateHired >= new DateTime(1980, 12, 21);
ctx.Employees.Join(ctx.Accounts, x=> EmployeeId,
y=>y.EmployeeId.EmployeeId (x, y) => new { x.LastName, y.AccountName });
ctx.Employees.OrderByDescending(y=>y.Salary).Take(1);
ctx.Employees.OrderByDescending(y=>y.Salary).Skip(10);
ctx.Accounts.Select(y=> new { y.AccountName, y.ClosedRevenue, TOTAL = y.PotentialRevenue - y.ClosedRevenue });
No comments:
Post a Comment