如何将此 SQL 语句转换为 LINQ to SQL,并在 select 语句中使用查询

发布于 2024-12-08 20:02:18 字数 454 浏览 0 评论 0原文

该表包含 staffidfirstnamelastnameemailsupervisoridsupervisorid 链接到同一张桌子上的staffid

select 
 StaffID,
 FirstName + ' ' + LastName as 'Name',
 Email,
 Supervisor = (Select FirstName + ' ' + LastName
                  From tblStaff staffinner 
            Where staffinner.StaffID = staffouter.SupervisorID)
from tblStaff staffouter

The table has staffid, firstname, lastname, email, supervisorid. The supervisorid is linked to the staffid on the same table.

select 
 StaffID,
 FirstName + ' ' + LastName as 'Name',
 Email,
 Supervisor = (Select FirstName + ' ' + LastName
                  From tblStaff staffinner 
            Where staffinner.StaffID = staffouter.SupervisorID)
from tblStaff staffouter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2024-12-15 20:02:18
var staff = from s1 in tblStaff
            select new
            {
               StaffID = s1.StaffID,
               Name = s1.FirstName + " " + s1.LastName,
               Email = s1.Email,
               Supervisor = (from s2 in tblStaff
                            where s2.StaffID == s1.SupervisorID
                            select s2.FirstName + " " + s2.LastName).First()
            };
var staff = from s1 in tblStaff
            select new
            {
               StaffID = s1.StaffID,
               Name = s1.FirstName + " " + s1.LastName,
               Email = s1.Email,
               Supervisor = (from s2 in tblStaff
                            where s2.StaffID == s1.SupervisorID
                            select s2.FirstName + " " + s2.LastName).First()
            };
羞稚 2024-12-15 20:02:18
var staff = tblStaff
            .Select(a => new
                         {
                          StaffID = a.StaffID,
                          Name = a.FirstName + " " + a.LastName,
                          Email = a.Email,
                          Supervisor = tblStaff
                                       .Where(b => b.StaffID = a.SupervisorID)
                                       .Select(b => b.FirstName + " " + b.LastName)
                                       .First()
                         };
var staff = tblStaff
            .Select(a => new
                         {
                          StaffID = a.StaffID,
                          Name = a.FirstName + " " + a.LastName,
                          Email = a.Email,
                          Supervisor = tblStaff
                                       .Where(b => b.StaffID = a.SupervisorID)
                                       .Select(b => b.FirstName + " " + b.LastName)
                                       .First()
                         };
尘世孤行 2024-12-15 20:02:18

你真的需要一个子选择,还是一个自连接就足够了?

var staff = from person in tblStaff
            join supervisor in tblStaff on person.StaffID equals supervisor.SupervisorID
            select new 
            {
                person.StaffID,
                Name = person.FirstName + ' ' + person.LastName,
                person.Email,
                Supervisor = supervisor.FirstName + ' ' + supervisor.LastName
            };

如果需要进行外连接,请使用 DefaultIfEmpty()。

Do you really need a subselect, or would a self join suffice here?

var staff = from person in tblStaff
            join supervisor in tblStaff on person.StaffID equals supervisor.SupervisorID
            select new 
            {
                person.StaffID,
                Name = person.FirstName + ' ' + person.LastName,
                person.Email,
                Supervisor = supervisor.FirstName + ' ' + supervisor.LastName
            };

If you need to do an outer join, use DefaultIfEmpty().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文