gpt4 book ai didi

c# - 如何将此查询更改为 linq to sql?

转载 作者:太空宇宙 更新时间:2023-11-03 20:40:24 25 4
gpt4 key购买 nike

查询:

select emp.empname as Name, dep.depname as Department 
from Employee as emp
inner join Department as dep on emp.depid=dep.depid
where emp.id='2'

如何将其更改为 linq to sql?

最佳答案

var id = 2;
var qry = from e in db.Employees
where e.Id == id
select new {
Name = e.EmpName,
Department = e.Department.DepName
};

(假设员工和部门之间的父关联在 DBML 中定义)

如果您恰好期望这样一个:

var record = qry.Single();
Console.WriteLine(record.Name);
Console.WriteLine(record.Department);

关于c# - 如何将此查询更改为 linq to sql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3059695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com