ASP.NET Linq To SQL和嵌套选择

时间:2010-06-24 15:55:09

标签: asp.net linq-to-sql nested-queries

我正在使用.NET 4,我只是想知道是否有一种优化的方法来实现以下目标。

    Public Function GetUserByOpenID(ByVal claimedidentifier As String) As User Implements IUserRepository.GetUserByOpenID
        Dim user = (From u In dc.Users
                    Where u.ID = (From o In dc.OpenIDs
                                  Where o.ClaimedIdentifier = claimedidentifier
                                  Select o.UserID).FirstOrDefault
                    Select u)
        Return user
    End Function

2 个答案:

答案 0 :(得分:1)

假设所有用户在OpenID中都有匹配的ID:

Dim user = (From u in dc.Users
            Join o in dc.OpenIDs On u.ID Equals o.UserId
            Where o.ClaimedIdentifier = claimedidentifier
            Select u).FirstOrDefault()

答案 1 :(得分:0)

Dim user = dc.OpenIDs
    .Where(o => o.ClaimedIdentifier == claimedidentifier)
    .Select(o => o.User)

我偏爱lambdas,我自己......