如何在linq中使用union从子查询中获取记录

发布于 2024-08-25 17:30:18 字数 324 浏览 6 评论 0原文

sql =  " SELECT * FROM userDetail ";
sql += " WHERE userId IN ";
sql += " (SELECT friendId FROM userFriends ";
sql += " WHERE approvalStatus='True' AND userId=" + userId;
sql += " UNION";
sql += " SELECT userId FROM userFriends ";
sql += " WHERE approvalStatus='True' AND friendId=" + userId + ")"; 
sql =  " SELECT * FROM userDetail ";
sql += " WHERE userId IN ";
sql += " (SELECT friendId FROM userFriends ";
sql += " WHERE approvalStatus='True' AND userId=" + userId;
sql += " UNION";
sql += " SELECT userId FROM userFriends ";
sql += " WHERE approvalStatus='True' AND friendId=" + userId + ")"; 

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

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

发布评论

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

评论(1

时光匆匆的小流年 2024-09-01 17:30:18

在 LINQ 中,您可能会这样:

var approvedUsers = db.UserFriends.Where(p => p.ApprovalStatus == "True");
var userIds = from p in approvedUsers
              where p.UserId == userId || p.FriendId = userId
              select p.UserId;

var friendsAndUser = db.UserDetails
                       .Where(detail => userIds.Contains(detail.UserId));

或者,使用联接:

var query = from user in db.UserFriends
            where p.ApprovalStatus == "True"
            where p.UserId == userId || p.FriendId == userId
            join detail in db.UserDetails on user.UserId equals detail.UserId
            select detail;

我怀疑这些都不会使用联合。您可以使用 LINQ 的联合,如下所示:

var approvedUsers = db.UserFriends.Where(p => p.ApprovalStatus == "True");
var userIds = from p in approvedUsers
              where p.UserId == userId
              select p.UserId;
var friendIds = from p in approvedUsers
                where p.FriendId = userId
                select p.UserId;
var allIds = userIds.Union(friendIds);
var friendsAndUser = db.UserDetails
                       .Where(detail => userIds.Contains(detail.UserId));

...但这很麻烦。我可能会加入。

In LINQ, you could be something like:

var approvedUsers = db.UserFriends.Where(p => p.ApprovalStatus == "True");
var userIds = from p in approvedUsers
              where p.UserId == userId || p.FriendId = userId
              select p.UserId;

var friendsAndUser = db.UserDetails
                       .Where(detail => userIds.Contains(detail.UserId));

Alternatively, use a join:

var query = from user in db.UserFriends
            where p.ApprovalStatus == "True"
            where p.UserId == userId || p.FriendId == userId
            join detail in db.UserDetails on user.UserId equals detail.UserId
            select detail;

I suspect neither of these would use a union. You could use a union with LINQ, like this:

var approvedUsers = db.UserFriends.Where(p => p.ApprovalStatus == "True");
var userIds = from p in approvedUsers
              where p.UserId == userId
              select p.UserId;
var friendIds = from p in approvedUsers
                where p.FriendId = userId
                select p.UserId;
var allIds = userIds.Union(friendIds);
var friendsAndUser = db.UserDetails
                       .Where(detail => userIds.Contains(detail.UserId));

... but that's a lot of fuss. I'd probably go with the join.

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