如何编写将此查询转换为lamda表达式?
from lst in from employee in dbe.employees
join ordermaster in dbe.ordermasters
on employee.emp_id
equals ordermaster.emp_id
select new
{
emp_name = employee.emp_name,
emp_id = ordermaster.emp_id,
order_date = ordermaster.Order_date
}
where EntityFunctions.DiffMonths(lst.order_date, dt) == 3
select lst;
答案 0 :(得分:0)
如果要选择列表,则可以使用:
var x = UtAccuralLstForComp.Where(x => x.VendorName == Name).ToList();
这将返回对象列表。
如果要选择一个值,则可以使用:
var x = UtAccuralLstForComp.Where(x => x.VendorName ==名称).Select(x => x.VendorNumber);
这将仅返回一个值。
答案 1 :(得分:0)
首先,欢迎来到!
我猜您正在使用LINQ-to-Entities,并鼓励您保留当前使用的语法。当涉及到加入时,我个人认为它更具可读性。但是如果您要将表达式转换为lambda,则应该等效:
// models just for testing...
var employees = new List<Employee>()
{
new Employee()
{
emp_id = 1,
emp_name = "Name 1"
},
new Employee()
{
emp_id = 2,
emp_name = "Name 1"
},
new Employee()
{
emp_id = 3,
emp_name = "Name 1"
}
};
var ordermasters = new List<OrderMaster>()
{
new OrderMaster()
{
emp_name = "Name 1",
emp_id = 1,
Order_date = DateTime.Now
},
new OrderMaster()
{
emp_name = "Name 3",
emp_id = 3,
Order_date = DateTime.Now.AddDays(1)
}
};
var result = employees.Join(ordermasters, // Replace employees and ordermasters with your tables.
e => e.emp_id,
o => o.emp_id,
(empl, ord) => new { Employees = empl, OrderMasters = ord })
.Where(arg => EntityFunctions.DiffMonths(arg.OrderMasters.Order_date, dt) == 3)
.Select(arg => new
{
emp_name = arg.Employees.emp_name,
emp_id = arg.OrderMasters.emp_id,
order_date = arg.OrderMasters.Order_date
});
就LINQ而言,它应该返回相同的结果。如果将其转换为相同的SQL,则完全不同。