C# LINQ 中的外部应用

发布于 2024-09-05 02:33:09 字数 250 浏览 5 评论 0原文

如何在LINQ中实现外部应用?我遇到了一点问题。

这是我正在使用的 SQL 查询。

SELECT u.masterID
      ,u.user
      ,h.created
FROM dbo.Users u
OUTER APPLY (SELECT TOP 1 * FROM UserHistory h where h.masterID = u.masterID ORDER BY created DESC) h

How can I achieve Outer Apply in LINQ? I'm having a bit of a problem.

Here's the SQL Query I'm using.

SELECT u.masterID
      ,u.user
      ,h.created
FROM dbo.Users u
OUTER APPLY (SELECT TOP 1 * FROM UserHistory h where h.masterID = u.masterID ORDER BY created DESC) h

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

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

发布评论

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

评论(3

爱*していゐ 2024-09-12 02:33:09
from u in Users 
join UserHistory on u.masterID equals h.masterID into h 
select new {
  u.masterID,
  u.user,
  Created = h.Select(x => x.created).OrderByDescending(c => c).FirstOrDefault()
}

或者,通过关联:

from u in Users
let created = u.UserHistories.Select(x => x.created).OrderByDescending(c => c).FirstOrDefault()
select new
{
  u.masterID,
  u.user,
  Created = created
}
from u in Users 
join UserHistory on u.masterID equals h.masterID into h 
select new {
  u.masterID,
  u.user,
  Created = h.Select(x => x.created).OrderByDescending(c => c).FirstOrDefault()
}

Or, with an association:

from u in Users
let created = u.UserHistories.Select(x => x.created).OrderByDescending(c => c).FirstOrDefault()
select new
{
  u.masterID,
  u.user,
  Created = created
}
转身以后 2024-09-12 02:33:09
from u in Users
join UserHistory on u.masterID equals h.masterID into h
select new {u.masterID, u.user, h.created.OrderByDescending().First()}
from u in Users
join UserHistory on u.masterID equals h.masterID into h
select new {u.masterID, u.user, h.created.OrderByDescending().First()}
独自←快乐 2024-09-12 02:33:09

外部应用产生左外部连接的结果,
查询应该是:

var q =
from u in db.Users
join h in db.UserHistory on u.masterID equals h.masterID into ps
from p in ps.DefaultIfEmpty()
select new { 
    masterID = u.masterID
    ,user = u.user
    ,created = ps.OrderByDescending(x=>x.created).FirstOrDefault()==null?null:ps.OrderByDescending(x=>x.created).First().created
};

Outer Apply produces results of left outer join,
the query should be:

var q =
from u in db.Users
join h in db.UserHistory on u.masterID equals h.masterID into ps
from p in ps.DefaultIfEmpty()
select new { 
    masterID = u.masterID
    ,user = u.user
    ,created = ps.OrderByDescending(x=>x.created).FirstOrDefault()==null?null:ps.OrderByDescending(x=>x.created).First().created
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文