我想在C#和VB.NET中得到建议
我有两个表Employees和CafeLogs。有些员工可以是收银员,也可以是咖啡店的顾客。
表格结构:
表关系:
员工表数据:
EmployeeId,FirstName,LastName
5,布朗,雷克萨斯
CafeLogs表数据:
LogId,CashierId,EmployeeId,Value
通缉结果:我想从一个查询中显示收银员名称和员工姓名:
7,Steven Gerald,Lours John,1
现在我知道如何只选择LogId,员工的姓名和价值,而不选择收银员名称。
Dim query = From log In db.CafeLogs _
Join emp In db.Employees On emp.EmployeeId Equals log.EmployeeId _
Select log.LogId, emp.FirsName, emp.LastName, log.Value
答案 0 :(得分:6)
这应该在C#中执行:
var query = from log in db.CafeLogs
join emp in db.Employees on log.EmployeeId equals emp.EmployeeId
join cas in db.Employees on log.CashierId equals cas.EmployeeId
select new
{
log.LogId,
Employee = emp.FirstName + " " + emp.LastName,
ChargedBy = cas.FirstName + " " + cas.LastName,
log.Value
};
和VB版本:
Dim query = From log In db.CafeLogs _
Join emp In db.Employees On log.EmployeeId Equals emp.EmployeeId _
Join cas In db.Employees On log.CashierId Equals cas.EmployeeId _
Select _
log.LogId, _
Employee = emp.FirstName & " " & emp.LastName, _
ChargedBy = cas.FirstName & " " & cas.LastName, _
log.Value