LINQ 和子查询

发布于 2024-11-01 18:22:53 字数 159 浏览 1 评论 0原文

我将如何在 LINQ 中执行以下操作?

select fkUniqueID
from tblUserRights
where fkUniqueID =
(select PkUserID
from Users
where UserID = 'mike')

How would I do the following in LINQ?

select fkUniqueID
from tblUserRights
where fkUniqueID =
(select PkUserID
from Users
where UserID = 'mike')

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

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

发布评论

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

评论(2

野稚 2024-11-08 18:22:53

我假设您正在使用 LINQ to SQL 或类似的东西。

理想情况下,使用连接:

var query = from user in db.Users
            where user.UserID == "mike"
            join userRight in db.UserRights
              on user.PkUiserID equals userRight.FkUniqueID
            select userRight;

或者,如果您确实想使用子查询:(

var mikeIDs = from user in db.Users
              where user.UserID == "mike"
              select user.PkUserID;

var query = from userRight in db.UserRights
            where mikeIDs.Contains(userRight.fkUniqueID)
            select userRight;

请注意,LINQ 是惰性的,因此这实际上不会执行第一部分的 SQL 查询。)

当然,如果您已经掌握了关系设置完毕后,您可以使用:

var rights = db.Users.Single(u => u.UserID == "mike").UserRights;

...如果没有这样的用户,那么这会很糟糕,并且它可能会进行两个数据库查询。

I assume you're using LINQ to SQL or something similar.

Ideally, with a join:

var query = from user in db.Users
            where user.UserID == "mike"
            join userRight in db.UserRights
              on user.PkUiserID equals userRight.FkUniqueID
            select userRight;

Alternatively, if you really want to use a subquery:

var mikeIDs = from user in db.Users
              where user.UserID == "mike"
              select user.PkUserID;

var query = from userRight in db.UserRights
            where mikeIDs.Contains(userRight.fkUniqueID)
            select userRight;

(Note that LINQ is lazy, so this won't actually execute a SQL query for the first part.)

Of course, if you've got your relationships set up, you can just use:

var rights = db.Users.Single(u => u.UserID == "mike").UserRights;

... that will go bang if there's no such user though, and it'll probably make two DB queries.

无风消散 2024-11-08 18:22:53

类似这个伪 LINQ 的东西应该可以工作(它实际上是问题的翻译)

var uniqueIDs = from userRight in tblUserRights
                where fkUniqueID = (from user in Users 
                                    where user.UserID = 'mike' 
                                    select user.pkUserID).First()
                select userRight.fkUniqueID 

在 SQL 中,我们不必使用像 First() 构造(一个 Top 1< /code> 将是 SQL 等效项),因为 SQL 将尝试与我们合作,如果只有一条记录(或根本没有记录),则可以正常工作,如果有两条或更多记录,则抛出描述性错误。

在 C# 中,这将被转换为编译时错误,因为我们正在与 T 进行比较,并且查询返回 IEnumerable
First() 显式返回集合中的第一个 T,如果结果中没有元素,则抛出错误。

Something along the lines of this pseudo-LINQ should work (it's practically a translation of the problem)

var uniqueIDs = from userRight in tblUserRights
                where fkUniqueID = (from user in Users 
                                    where user.UserID = 'mike' 
                                    select user.pkUserID).First()
                select userRight.fkUniqueID 

In SQL we do not have to use anything like the First() construct (a Top 1 would be a SQL equivalent) because SQL will try to work with us, working correctly if there is only one record (or no records at all), and throwing a descriptive error if there are two or more records.

In C#, that would be translated to a compile-time error, since we are comparing with a T, and the query returns a IEnumerable<T>.
First() explicitly returns the first T in the collection, or throws an error if there are no elements in the result.

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