EF FirstOfDefault查询的不是第一条记录问题

发布于 2021-12-06 12:28:39 字数 475 浏览 853 评论 2

这是代码 WxUser是外键

这是数据 这三条数据的WxUser除了第二个都是同一个

为什么查出的是最后一条 

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

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

发布评论

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

评论(2

各自安好 2021-12-07 23:16:35

你可以用.orderby(x=>x.ID)排序一下试试看呢

猫性小仙女 2021-12-06 21:32:10

SELECT 
    [Limit1].[ID] AS [ID], 
    [Limit1].[Name] AS [Name], 
    [Limit1].[Sex] AS [Sex], 
    [Limit1].[Phone] AS [Phone], 
    [Limit1].[Age] AS [Age], 
    [Limit1].[Category] AS [Category], 
    [Limit1].[Email] AS [Email], 
    [Limit1].[HostOrganization] AS [HostOrganization], 
    [Limit1].[Province] AS [Province], 
    [Limit1].[City] AS [City], 
    [Limit1].[Area] AS [Area], 
    [Limit1].[Address] AS [Address], 
    [Limit1].[Ip] AS [Ip], 
    [Limit1].[Country] AS [Country], 
    [Limit1].[Account] AS [Account], 
    [Limit1].[Password] AS [Password], 
    [Limit1].[CreateTime] AS [CreateTime], 
    [Limit1].[RegisterInfoId] AS [RegisterInfoId], 
    [Limit1].[HeadUrl] AS [HeadUrl], 
    [Limit1].[PassTime] AS [PassTime], 
    [Limit1].[Status] AS [Status], 
    [Limit1].[Message] AS [Message], 
    [Limit1].[Parent_ID] AS [Parent_ID], 
    [Limit1].[PdaUser_ID] AS [PdaUser_ID], 
    [Limit1].[WxUser_ID] AS [WxUser_ID], 
    [Limit1].[YzUser_ID] AS [YzUser_ID]
    FROM ( SELECT TOP (1) 
        [Extent1].[ID] AS [ID], 
        [Extent1].[Name] AS [Name], 
        [Extent1].[Sex] AS [Sex], 
        [Extent1].[Phone] AS [Phone], 
        [Extent1].[Age] AS [Age], 
        [Extent1].[Category] AS [Category], 
        [Extent1].[Email] AS [Email], 
        [Extent1].[HostOrganization] AS [HostOrganization], 
        [Extent1].[Province] AS [Province], 
        [Extent1].[City] AS [City], 
        [Extent1].[Area] AS [Area], 
        [Extent1].[Address] AS [Address], 
        [Extent1].[Ip] AS [Ip], 
        [Extent1].[Country] AS [Country], 
        [Extent1].[Account] AS [Account], 
        [Extent1].[Password] AS [Password], 
        [Extent1].[CreateTime] AS [CreateTime], 
        [Extent1].[RegisterInfoId] AS [RegisterInfoId], 
        [Extent1].[HeadUrl] AS [HeadUrl], 
        [Extent1].[PassTime] AS [PassTime], 
        [Extent1].[Status] AS [Status], 
        [Extent1].[Message] AS [Message], 
        [Extent1].[Parent_ID] AS [Parent_ID], 
        [Extent1].[PdaUser_ID] AS [PdaUser_ID], 
        [Extent1].[WxUser_ID] AS [WxUser_ID], 
        [Extent1].[YzUser_ID] AS [YzUser_ID]
        FROM  [dbo].[YzMembers] AS [Extent1]
        LEFT OUTER JOIN [dbo].[WxUsers] AS [Extent2] ON [Extent1].[WxUser_ID] = [Extent2].[ID]
        WHERE ([Extent2].[OpenId] = @p__linq__0) OR (([Extent2].[OpenId] IS NULL) AND (@p__linq__0 IS NULL))
    )  AS [Limit1]

 

这是EF生成的SQL 如果直接在数据库中执行反而是和我想要的结果是一致的

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